This simple tutorial demonstrates writing, deploying, and triggering an HTTP Cloud Function.
Objectives
- Write, deploy, and trigger an HTTP Cloud Function.
Costs
This tutorial uses billable components of Cloud Platform, including:
- Google Cloud Functions
Use the Pricing Calculator to generate a cost estimate based on your projected usage.
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Enable the Cloud Functions and Cloud Build APIs.
- Install and initialize the Cloud SDK.
- Prepare your development environment.
If you already have the Cloud SDK installed, update it by running the following command:
gcloud components update
Preparing the application
Clone the sample app repository to your local machine:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Cloud Functions sample code:
Node.js
cd nodejs-docs-samples/functions/helloworld/
Python
cd python-docs-samples/functions/helloworld/
Go
cd golang-samples/functions/helloworld/
Java
cd java-docs-samples/functions/helloworld/helloworld/
C#
cd dotnet-docs-samples/functions/helloworld/HelloWorld/
Ruby
cd ruby-docs-samples/functions/helloworld/
Take a look at the sample code:
Node.js
Python
Go
Java
C#
Ruby
Deploying the function
To deploy the function with an HTTP trigger, run the following command in the
directory that contains the sample code (or in the case of Java, the pom.xml
file):
Node.js
gcloud functions deploy helloGET \ --runtime nodejs10 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Node.js version:
nodejs10
nodejs12
nodejs14
(public preview)
Python
gcloud functions deploy hello_get \ --runtime python37 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Python version:
python37
python38
python39
(public preview)
Go
gcloud functions deploy HelloGet \ --runtime go111 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Go version:
go111
go113
Java
gcloud functions deploy java-http-function \ --entry-point functions.HelloWorld \ --runtime java11 \ --memory 512MB --trigger-http --allow-unauthenticated
C#
gcloud functions deploy csharp-http-function \ --entry-point HelloWorld.Function \ --runtime dotnet3 --trigger-http --allow-unauthenticated
Ruby
gcloud functions deploy hello_get --runtime ruby26 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Ruby version:
ruby26
ruby27
The --allow-unauthenticated
flag lets you reach the function
without authentication.
To require authentication, omit the flag.
Triggering the function
To make an HTTP request to your function, run the following command:
Node.js
curl "https://REGION-PROJECT_ID.cloudfunctions.net/helloGET"
Python
curl "https://REGION-PROJECT_ID.cloudfunctions.net/hello_get"
Go
curl "https://REGION-PROJECT_ID.cloudfunctions.net/HelloGet"
Java
curl "https://REGION-PROJECT_ID.cloudfunctions.net/java-http-function"
C#
curl "https://REGION-PROJECT_ID.cloudfunctions.net/csharp-http-function"
Ruby
curl "https://REGION-PROJECT_ID.cloudfunctions.net/hello_get"
where
REGION
is the region where your function is deployed. This is visible in your terminal when your function finishes deploying.PROJECT_ID
is your Cloud project ID. This is visible in your terminal when your function finishes deploying.
You can also visit the deployed function's endpoint in your browser to see the "Hello World!" message.
Cleaning up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Deleting the project
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
- In the Cloud Console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Deleting the Cloud Function
Deleting Cloud Functions does not remove any resources stored in Cloud Storage.
To delete the Cloud Function you created in this tutorial, run the following command:
Node.js
gcloud functions delete helloGET
Python
gcloud functions delete hello_get
Go
gcloud functions delete HelloGet
Java
gcloud functions delete java-http-function
C#
gcloud functions delete csharp-http-function
Ruby
gcloud functions delete hello_get
You can also delete Cloud Functions from the Google Cloud Console.