A couple of months back I built a proof of concept API Gateway + AWS Lambda function with Kotlin targeting the java8 runtime. I really just wanted to go through the steps to see what the code looked like. Theoretically it seemed pretty straight forward. Kotlin is designed to interoperate well with Java so wasn’t surprised that it mostly just worked.
As much as I love the JVM there are some negatives to running java8 runtime lambda functions vs node runtime. In particular:
- Cold start times for java8 are much higher (though performance more consistent)
- Java lambda functions need more resources (memory) than Node functions (this has been my experience — would love numbers if you have them)
- Java packages are generally much larger than Node packages
tl;dr — It’s alive!
So yeah. It works… Go clone away.
Digging into Code a bit
You may not be familiar with the
external modifier (I wasn’t before reading this). Basically, it tells the compiler that the implementation for this
handle which is a function that takes
event, context objects along with another
callback. I then define the function in the block that follows which essentially prints out the input and returns
Here you go… Not too bad right?
Calling from Lambda / Give me a hand(ler)
So as you may know, in order for Lambda to be able to invoke your function you need to defined a “handler” that it can call. I am not sure if it was user error (read: developer ignorance) but I couldn’t get Lambda to invoke this function directly. I tried a bunch of different combinations but couldn’t do figure it out so I created basically a bootstrap script that is my
This script is what Lambda calls and as you can see it just calls the
handle function in main.js.
Teaser on ts2kt script
external modifier. Turns out there is a project called ts2kt that can compile Typescript to Kotlin. We’ll explore that more in the future.
Get in touch
I’d love to hear from you! Ping me @floodfx on Twitter.