Can Kotlin compiled to Javascript run on AWS Lambda?

Photo by Markus Spiske on Unsplash

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
  • Java isn’t Javascript ;)

More important than any differences between Java and Node runtimes was the fact that I wanted to learn about Kotlin and how it compiled to Javascript and see if I could run it on AWS Lambda.

tl;dr — It’s alive!

So yeah. It works… Go clone away.

Digging into Code a bit

Here’s a gist of my Main.kt class which gets compiled into Javascript:

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 class or fun is defined elsewhere. This is kind of necessary when you want to have typesafe function invocations in Kotlin code but the function is implemented in Javascript.

Other than external it kind of reads like javascript. I am defining a variable handle which is a function that takes event, context objects along with another lambda called callback. I then define the function in the block that follows which essentially prints out the input and returns "Success!".

Show me the Javascript

Here you go… Not too bad right?

I mean it isn’t pretty but honestly anyone using Babel (de facto javascript compiler) should be used to “compiled” javascript looking a little, well, compiled.

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 index.js.

This script is what Lambda calls and as you can see it just calls the handle function in main.js.

Teaser on ts2kt script

After I got this working, I wanted to call more javascript code but I didn’t want to have to re-define any and all functions defined in javascript using the external modifier. Turns out there is a project called ts2kt that can compile Typescript to Kotlin. We’ll explore that more in the future.

IDE Recommendation

Sadly, Eclipse does not know how to handle code targeting Javascript… So for Kotlin targeting Javascript your best bet is to use IntelliJ.


Turns out you can run Javascript on AWS Lambda. :) I mean, it turns out you can run Kotlin code compiled into Javascript on AWS Lambda! It will be interesting to see how this continues to develop.

Get in touch

I’d love to hear from you! Ping me @floodfx on Twitter.

Like what you read? Give Donnie Flood a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.