Google Cloud Functions (Alpha)

The Google Cloud Functions adapter enables Spring Cloud Function apps to run on the Google Cloud Functions serverless platform. You can either run the function locally using the open source Google Functions Framework for Java or on GCP.

Getting Started

Let’s start with a simple Spring Cloud Function example:

public class CloudFunctionMain {

	public static void main(String[] args) {
		SpringApplication.run(CloudFunctionMain.class, args);

	public Function<String, String> uppercase() {
		return value -> value.toUpperCase();
Test locally

Start by adding the Maven plugin provided as part of the Google Functions Framework for Java.


Specify your configuration main class in resources/META-INF/MANIFEST.MF.

Main-Class: com.example.CloudFunctionMain

Then run the function:

mvn function:run

Invoke the HTTP function:

curl http://localhost:8080/ -d "hello"
Deploy to GCP

As of March 2020, Google Cloud Functions for Java is in Alpha. You can get on the whitelist to try it out.

To deploy to Google Cloud Function, you need to produce a fat jar using the Shade plugin, rather than the Spring Boot plugin.

First, if you already have the Spring Boot plugin in your pom.xml, remove it:

<!-- Remove this block by deleting or commenting it out -->

Then, add the Shade Plugin configuration to generate a fat jar when you run the mvn package command.

                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                    <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
If both Spring Boot plugin and Shade plugin are present, Shade plugin may be shading a Spring Boot produced JAR, resulting in a Fat JAR that’s unusable in Google Cloud Function. Don’t forget to remove the Spring Boot plugin!

Package the application.

mvn package

You should see the fat jar in target/deploy directory.

Make sure that you have the Cloud SDK CLI installed.

From the project base directory run the following command to deploy.

gcloud alpha functions deploy function-sample-gcp \
--entry-point org.springframework.cloud.function.adapter.gcloud.FunctionInvoker \
--runtime java11 \
--trigger-http \
--source target/deploy \
--memory 512MB

Invoke the HTTP function:

curl https://REGION-PROJECT_ID.cloudfunctions.net/function-sample-gcp -d "hello"

Sample Function

Go to the function-sample-gcp to try out a sample function that you can test locally or deploy to GCP.