DEV Community

Cover image for Creating an AWS Lambda Kotlin function
Gaute Meek Olsen
Gaute Meek Olsen

Posted on • Updated on • Originally published at gaute.dev

Creating an AWS Lambda Kotlin function

This article is part of the Twitter bot with Kotlin in AWS series showing how I created a Twitter bot for Vue 3 updates. But this article works as an independent article on how to create a Lambda function in Kotlin.

You could manage your AWS Lambda functions in the browser AWS console. But I'm going to use the AWS CLI as I believe that leads to less manual work.

AWS setup

  • You need to create an account and sign in to the AWS Management Console
  • Create an Administrator IAM User. Remember to copy the AWS Access Key ID and AWS Secret Access Key for later use.
  • Create a role and select AWS service and Lambda. Give permission to the resources the Lambda function is going to need. I have given permission to CloudWatch Logs, DynamoDB, and Secrets Manager.

Setting up the AWS CLI

Install the AWS CLI to your computer and verify it is installed by running aws --version in your terminal.

Now you need to configure the CLI by using the IAM user created previously.
Run aws configure in your terminal and enter the AWS Access Key ID and AWS Secret Access Key, region name (available codes can be found here) and output (json, yaml, text or table) where I use json.

Kotlin Project

I'm using IntelliJ as my editor. Now go ahead and create a new project and select gradle, Kotlin DSL build script (not required), and Kotlin/JVM.
Alt Text

Create file Main.kt that will hold our function we want to deploy to an AWS Lambda function.

class Main {
    fun runLambda() = "Hello World"
}
Enter fullscreen mode Exit fullscreen mode

We need to make sure that our .jar file which we are going to upload will be a fat jar, containing all our dependencies, by altering the build.gradle.kts file and adding this to tasks. We can also change the filename if we want.

jar {
    archiveFileName.set("twitter-bot-vue-3.jar")
    from(configurations
        .runtimeClasspath.get().files.filter { it.exists() }
        .map { if (it.isDirectory) it else zipTree(it) })
}
Enter fullscreen mode Exit fullscreen mode

Deploy to AWS

Build .jar:

gradlew build
Enter fullscreen mode Exit fullscreen mode

Create Lambda function (For the role parameter, you can copy the Role ARN from the setup):

aws lambda create-function --function-name twitter-bot-vue-3 --zip-file fileb://build/libs/twitter-bot-vue-3.jar --runtime java11 --role arn:aws:iam::<account_id>:role/<role>  --handler Main::runLambda --timeout 15 --memory-size 512
Enter fullscreen mode Exit fullscreen mode

Update Lambda function:

aws lambda update-function-code --function-name twitter-bot-vue-3 --zip-file fileb://build/libs/twitter-bot-vue-3.jar
Enter fullscreen mode Exit fullscreen mode

Invoke Lambda function:

aws lambda invoke --function-name twitter-bot-vue-3 output.txt
Enter fullscreen mode Exit fullscreen mode

Now go ahead to check the output.txt file to see if our function ran correctly.

Top comments (2)

Collapse
 
starpebble profile image
starpebble

There are one nonillion things we can all do with Java that can't simply be done with NodeJS. This is excellent. Thanks for increasing our serverless potential. It's not at all bad that the Lambda executes Kotlin on Java's VM.

Collapse
 
stephanatwhich profile image
Stephan Schroeder

I actually created an AWS Lambda in Kotlin myself: emailsender. Since it's for my private site and doesn't require a lot of updates (it's basically deploy once, update (potentially) never), I didn't bother setting up a pipeline (install/configure the AWS CLI). I just made sure that my Kotlin project compiles to Java8 bytecode (by now a Java11 is supported as well) and uploaded the resulting jar-file directly to Lambda via the AWS web-interface into a project using the Java8-Runtime.