Creating Google Cloud Functions with Kotlin

Build a Kotlin HTTP function and deploy it to GCP

In May 2020, Google announced that Java 11 was coming to Google Cloud Functions. Naturally, my first thought was “Excellent, but can I write functions in other JVM-based languages like Kotlin?”

Thankfully, there is no such limitation. In fact, you can write your functions in any JVM language of your choosing.

Source code for this example can be found on GitHub:

Contribute to codenerve-com/kotlin-google-cloud-function development by creating an account on GitHub.


You'll need some things to follow along with this example:

  • Java 11
  • Gradle
  • Access to GCP with a sample project created
  • gcloud installed and authenticated
  • The source code cloned and imported into your IDE

Main Function

The main function is a straightforward one to get us started -- a basic HTTP endpoint that will return the string "FUNCTION COMPLETE" when triggered.

package com.codenerve.function

import mu.KotlinLogging

class App : HttpFunction {

    private val logger = KotlinLogging.logger {}

    override fun service(request: HttpRequest, response: HttpResponse) { { "hello world" }
        response.writer.write("FUNCTION COMPLETE")

This class extends HttpFunction from the functions-framework library, and the service function takes an HttpRequest and a HttpResponse object as parameters.

For non-HTTP ways to trigger a cloud function, you can use a RawBackgroundFunction or a typed variant.

For example BackgroundFunction<PubSubMessage>

Some other options for triggering functions include Cloud Pub/Sub, Cloud Storage, Cloud Firestore, and various Firebase-based triggers.

You can find examples for other triggering mechanisms in the functions-framework-java readme.


Gradle Build File

For this example, we are using Gradles Kotlin DSL to configure our project.

import java.lang.invoke.MethodHandles.invoker

val invoker by configurations.creating

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.3.72"
    id("com.github.johnrengelman.shadow") version "6.0.0"

repositories {

dependencies {


application {
    mainClassName = "com.codenerve.function.AppKt"

task<JavaExec>("runFunction") {
    main = ""
    inputs.files(configurations.runtimeClasspath, sourceSets["main"].output)
        "--target", project.findProperty("") ?: "com.codenerwve.function.App",
        "--port", project.findProperty("runFunction.port") ?: 8080
    doFirst {
        args("--classpath", files(configurations.runtimeClasspath, sourceSets["main"].output).asPath)

tasks.named("build") {

task("buildFunction") {
    copy {
        from("build/libs/" + + "-all.jar")


There are a few critical dependencies

  • Functions-framework-api allows us to write lightweight functions that run in many different environments, including google cloud functions and cloud run.
  • Java-function-invoker enables us to run the function locally for testing.

runFunction task

The runFunction task in the Gradle build file triggers the java-function-invoker which wraps the function and serves it using a jetty web server.

To run the function locally, call runFunction using the Gradle wrapper:

 ./gradlew runFunction
 ❯ ./gradlew runFunction 

> Task :runFunction
INFO: Serving function...
Oct 06, 2020 9:10:09 PM logServerInfo
INFO: Function: com.codenerve.function.App
Oct 06, 2020 9:10:09 PM logServerInfo
INFO: URL: http://localhost:8080/
<==========---> 80% EXECUTING [1m 13s]
> :runFunction

(logging shortened for readability)

Optionally, you can override some args:

./gradlew runFunction -PrunFunction.port=8080

buildFunction Task

The buildFunction task in the Gradle build file works with the Gradle Shadow plugin to create a fat jar and copy it to the build/deploy directory ready to be uploaded to GCP Cloud Storage.

To execute this, use the Gradle wrapper:

❯ ./gradlew buildFunction 

10 actionable tasks: 9 executed, 1 up-to-date

Deploying with gcloud

So you've tested your function locally, you've built it, and now its time to deploy. Deploying can be achieved easily with the gcloud command-line utility.

First, ensure you are deploying to the GCP Project of your choice:

gcloud config get-value project my-functions-project

Then select the region you wish to deploy to :

gcloud config set functions/region europe-west1

Lastly, deploy the function:

gcloud functions deploy my-test-function \
--entry-point=dev.mwhyte.function.App \
--source=build/deploy --runtime=java11 --trigger-http \

Note: The entry-point argument is the fully qualified class name of the function, and the source is the location of our fat jar.

If you open the GCP console and navigate to Cloud Functions, you'll see the function:

From here you can open it and view various information, including the trigger. Alternatively, you can run a describe from gcloud:

gcloud functions describe my-test-function

By hitting the trigger URL, you will see the function return the expected response:

❯ curl


We've seen just how easy it is to deploy a kotlin function to GCP. To explore the topic further. I'd recommend the following documentation:

Cloud Functions documentation | Cloud Functions Documentation
Small, single-purpose functions.
FaaS (Function as a service) framework for writing portable Java functions - GoogleCloudPlatform/functions-framework-java
Running a function with Gradle & Kotlin (build.gradle.kts) · Issue #35 · GoogleCloudPlatform/functions-framework-java
Thanks for putting all this together! I was trying to get the local invoker working on my local machine, which is setup using kotlin (1.3.72) and gradle (6.4.1) via the build.gradle.kts file. The G...

Michael Whyte

Michael Whyte