Using Kotlin for Cloud Functions in the Google Cloud Platform
By: Daniel Bunte
Tags: Cloud Functions, Google Cloud Platform, Kotlin, Software Development

Recently, I’ve played around with Cloud Functions on the Google Cloud Platform for a private project. Since this was the first time doing Cloud Functions, I started out by reading the documentation. It offers a lot of different Quickstart guides for technologies such as Node.js, Python, Go, .Net, and Java. I was a little disappointed not to find Kotlin in this list, as Google made it the preferred language for Android Development in 2019 and has ongoing cooperation with Jetbrains.

I started by implementing the first function in Java and things went quite well. I set up a Maven project, included the required dependencies, and added some code. You can use Gradle as well if you like.

Wait… Maven… Gradle… If I can use such a build-management tool in Cloud Functions, can’t I reconfigure it to support Kotlin development? Let’s try it! For this quick tutorial, I assume that you have basic knowledge of the Google Cloud Platform.

Google Cloud Functions with Java

For the sake of completeness, I’ll post the HelloWorld Java sources from the Google docs that I used to set up my initial function. We start with a simple HelloWorld.java class that implements HttpFunction from the com.google.cloud.functions package. Then we add the appropriate pom.xml, and we’re good to go.

Google Cloud Functions with Kotlin

Now let’s have a look at how we can use Kotlin to write Cloud Functions. First, we must convert the class from the Java example to Kotlin. Caution, if you use the automatic conversion functionality from within IntelliJ, as it will most likely add imports for java.net.HttpRequest/Response and make the parameters of the “service” function nullable.

Once the class is converted, we should move the file from the original path /src/main/java/functions/HelloWorld.java to the new path /src/main/kotlin/functions/HelloWorld.kt.

For the pom.xml, there’s some more stuff to do.

Line 13: Add the desired Kotlin version as separate property — in my case 1.4.20 Lines 27–37: Add dependencies to the Kotlin standard library and the Kotlin testing package Lines 57–85: Add the Kotlin Maven Build plugin

Configuring the Kotlin Function in Google Cloud Functions

First of all, we must configure a new function in GCP. For this, go to your project and select Cloud Functions from the menu.

It might be necessary to enable the Cloud Functions first before you’re able to use them. In that case, you might be shown a button “Enable Cloud Functions.”

Cloud Functions can be found in the “Compute” section of the menu. This view also shows it under the pinned products.

If this is your first function, you’ll be shown a card like the one below. Click on “Create Function” to get started.

It takes a while for the Cloud Functions to be prepared.

If you have already created Cloud Functions for this project before, click on “Create Function” in the toolbar.

The toolbar allows for managing your functions.

Give a name to your new function and leave the rest on default for this tutorial. Of course, you can adjust the configuration here if you like, but things like memory and timeouts can also be configured afterward.

Creating a new Cloud Function is straight forward.

When configuring the new Cloud Function, you would still select Java 11 as the runtime since Kotlin runs on the JVM (at least in the context of this article).

For the source code, we will work with the inline-editor for now. Click on the + sign under the “Source code” combo box to add a new file. Include the path in the input field, such as /src/main/kotlin/functions/HelloWorld.kt, and the directory structure will be created for you. Paste the code from above and do the same for the pom.xml in the root directory.

The Inline Editor allows for basic quick editing of source files.

Once you hit “Deploy,” the function will be created and is ready to use after a short moment.

Getting the function deployed sometimes takes up to two minutes.

Conclusion

Even though Kotlin isn’t mentioned anywhere in the officially supported languages, it’s quite easy to set up Kotlin Cloud Functions in the Google Cloud Platform. I’m certain this will apply to other JVM-languages such as Scala and Groovy as well.

I haven’t tried this yet on any other Cloud Platform. I’m eager to know if it works there too. So if you have some time to try, please let me know in the comments! If you found this article helpful, consider giving some claps :)