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
Copy cd $HOME
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/functions/helloworld/helloworld
Build
Run Locally
Copy mvn function:run
# In a different tab, trigger the function:
curl localhost:8080
Deploy
Enable API
Copy gcloud services enable cloudfunctions.googleapis.com
Deploy
Copy gcloud functions deploy helloworld --trigger-http \
--runtime=java11 \
--entry-point=functions.HelloWorld \
--allow-unauthenticated
Connect
Once a HTTP function is deployed, you can connect to it using curl
. You can also find the URL:
Copy gcloud functions describe helloworld --format= 'value(httpsTrigger.url)'
Trigger the function with curl
:
Copy URL = $( gcloud functions describe helloworld --format= 'value(httpsTrigger.url)' )
curl ${URL}
Alternatively, you can also use gcloud
:
Copy gcloud functions call helloworld
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:
Copy gcloud functions deploy helloworld --trigger-http \
--runtime=java11 \
--memory=512M
--entry-point = functions.HelloWorld \
--allow-unauthenticated
Learn More