JVM Languages
It's possible to write your function in using different JVM languages (such as Kotlin, Groovy, or Scala) as long as they conform to the following rules:
The function is a public class that implements one of the function interfaces (
HttpFunction
,BackgroundFunction
, orRawBackgroundFunction
) and has a public no-argument constructor.If you are deploying from source:
- It can be built from Maven.
- The build file contains all the plugins to produce compiled classes.
If you are deploying from a pre-built JAR:
- You can use any build tools to produce this JAR.
- The pre-built JAR must be a Fat JAR with all of its dependency classes, or
its manifest must contain a
Class-Path
entry with the relative locations of jars containing those dependency classes.
HTTP function examples
You use HTTP functions when you want to invoke
your function via an HTTP(S) request. The following examples output the message
"Hello World!"
Kotlin
Groovy
Scala
pom.xml
file for HTTP examples
Here are the pom.xml
files for the above samples:
Kotlin
Groovy
Scala
Deploying the HTTP functions
Kotlin
gcloud functions deploy kotlin-helloworld --entry-point functions.KotlinHelloWorld --runtime java21 --trigger-http --allow-unauthenticated --memory 512MB
Groovy
gcloud functions deploy groovy-helloworld --entry-point functions.GroovyHelloWorld --runtime java21 --trigger-http --allow-unauthenticated --memory 512MB
Scala
gcloud functions deploy scala-helloworld --entry-point functions.ScalaHelloWorld --runtime java21 --trigger-http --allow-unauthenticated --memory 512MB
Event-driven function examples
You use event-driven functions when you want to have your Cloud Run function invoked indirectly in response to an asynchronous event, such as a message on a Pub/Sub topic, a change in a Cloud Storage bucket, or a Firebase event.
There are two types of event-driven functions: background functions and CloudEvent functions. The JVM languages only support background functions.
Kotlin
When developing background functions, you define classes for the events triggering your functions. However, GSON marshalling may not work out of the box for Kotlin, if your event class doesn't follow certain guidelines.
In your Kotlin event class, properties must conform to these guidelines:
- They are able to be set to
null
. - They don't have a default value assigned to them.
- They are not delegate properties.
Another approach is to create your event classes in Java, and use them from your Kotlin function class.
Groovy
Scala
Deploying the background functions
Kotlin
gcloud functions deploy kotlin-hello-pubsub --entry-point functions.KotlinHelloPubSub --runtime java21 --trigger-topic my-topic --allow-unauthenticated --memory 512MB
Groovy
gcloud functions deploy groovy-hello-pubsub --entry-point functions.GroovyHelloPubSub --runtime java21 --trigger-topic my-topic --allow-unauthenticated --memory 512MB
Scala
gcloud functions deploy scala-hello-pubsub --entry-point functions.ScalaHelloPubSub --runtime java21 --trigger-topic my-topic --allow-unauthenticated --memory 512MB
Test the background examples
You can test the background examples as follows:
Publish a message to your Pub/Sub Topic to trigger your function:
gcloud pubsub topics publish my-topic --message Flurry
Look at the logs:
gcloud functions logs read --limit 10
You should see something like this, with a message that includes the name you published to the Pub/Sub topic:
D my-function ... Function execution started
I my-function ... Hello Flurry!
D my-function ... Function execution took 39 ms, finished with status: 'ok'