Serverless Kotlin: How to run a Kotlin action on OpenWhisk

Kotlin, while not being entirely new, has recently been announced as an official language to develop on Android by Google. Being a Scala developer I watched Kotlin’s development quite closely, as it is another very promising functional programming language. With Google’s announcement it looks like functional programming and its principles will get even more popular and widespread. This is great!

As Kotlin is a JVM language with great Java interoperability I wanted to try to run it on OpenWhisk, so I can develop my actions in a functional way. Well, I could also run Scala on OpenWhisk, but where is the fun of experimenting then?

TL;DR: If you’re already familiar with OpenWhisk, Java actions and Kotlin, go straight to my example repository to get your feet wet.

Kicking the tires: Setting up the project

I wanted to develop in the most standard way there is, so I created a Gradle project right from IntelliJ IDEA. Make sure to use Java 8 to get all the nice features of Kotlin you can get. Doing so yields a build.gradle like this:

So far so good!

Shifting to the first gear: Writing a minimal action

OpenWhisk actions are JSON-in/JSON-out. To be precise: They get a JSON object of arguments and need to return a JSON object containing the result. Java actions on OpenWhisk (which we piggyback on) use Google’s GSON library for JSON handling, so we’ll use the same for our Kotlin action. I chose Hello.kt as the filename for my action, this will be important later:

But that code looks quite Java-like. For instance it makes use of mutability. We don’t want no mutability! Using Kotson, GSON bindings for Kotlin, we can achieve a much more immutable approach:

For this to work we’ll also need to add GSON and Kotson to our build dependencies:

compile "com.google.code.gson:gson:2.8.1"
compile "com.github.salomonbrys.kotson:kotson:2.5.0"

We’re almost done! Gradle will by default not compile all dependencies into a single jar but instead place precompiled jars along your own application and load them via the classpath. For OpenWhisk though, we need a single jar, also called fat-jar, which contains all the dependencies. To achieve that, we add:

jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

Hitting the throttle: Using the action

We’re almost finished here. The next step is compiling the action via Gradle:

$ gradle jar

Easy enough! The resulting jar file will be placed in build/libs. For my project, that’s: hello-kotlin.jar.

Now we create the action in OpenWhisk:

$ wsk action create kotlin build/libs/hello-kotlin.jar --main HelloKt

Note the main option. Kotlin compiles to JVM Bytecode class files. Remember the action file was named Hello.kt, which after compilation results in HelloKt.class. That’s the class OpenWhisk should load to call the main function from.

We’re done! Now we can invoke the action:

$ wsk action invoke kotlin -r -p name “Markus”
{
“message”: “Hello Markus!”
}

Summary

This turned our way easier than I expected! Find the whole code in my openwhisk-kotlin repository to start even quicker.

With no prior knowledge of how to build a Kotlin project it took me about an hour to accomplish this minimal example of using Kotlin on top of OpenWhisk, without the language being officially supported. I think that’s awesome and speaks for the interoperability of Kotlin with Java and the flexibility of OpenWhisk.

One of the great benefits of using Kotlin is, that you can write Android apps and their backends in the same language. That was possible with Java before, but Kotlin is in my opinion a much more modern language and thus more fun to work with. And we’re all about fun, aren’t we?


If you liked this article, give it one of the nice green hearts 💚, recommend it to your friends and follow OpenWhisk. We have a lot more stories to tell and always great stuff coming up. Stay tuned 🚀

Markus Thömmes is a leading contributor to the Apache OpenWhisk project. He loves serverless and ☁️ in general. Follow him here on Medium and on Twitter for more deep-dive information about serverless platforms.

Like what you read? Give Markus Thömmes a round of applause.

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