Cloud Functions
Deploy a simple HTTP function.
Cloud Function is a scalable, pay as you go, Functions-as-a-Service (FaaS).
Spring Cloud Functions has pre-GA support for Cloud Functions for Java 11. See Spring Cloud Functions Reference Documentation for more details.
This guide currently uses a non-Spring example for Cloud Functions.

Getting Started

Clone

1
cd $HOME
2
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
3
cd java-docs-samples/functions/helloworld/helloworld
Copied!

Build

1
mvn package
Copied!

Run Locally

1
mvn function:run
2
3
# In a different tab, trigger the function:
4
curl localhost:8080
Copied!

Deploy

Enable API

1
gcloud services enable cloudfunctions.googleapis.com
Copied!

Deploy

1
gcloud functions deploy helloworld --trigger-http \
2
--runtime=java11 \
3
--entry-point=functions.HelloWorld \
4
--allow-unauthenticated
Copied!

Connect

Once a HTTP function is deployed, you can connect to it using curl. You can also find the URL:
1
gcloud functions describe helloworld --format='value(httpsTrigger.url)'
Copied!
Trigger the function with curl:
1
URL=$(gcloud functions describe helloworld --format='value(httpsTrigger.url)')
2
3
curl ${URL}
Copied!
Alternatively, you can also use gcloud:
1
gcloud functions call helloworld
Copied!

Additional Configurations

By default, Cloud Functions will deploy to the smallest 256MB instance. You can specify a larger instance and configure environment variables with the gcloud CLI:
1
gcloud functions deploy helloworld --trigger-http \
2
--runtime=java11 \
3
--memory=512M
4
--entry-point=functions.HelloWorld \
5
--allow-unauthenticated
Copied!

Learn More

Last modified 1yr ago