This page shows you how to create and deploy a Cloud Function using the
gcloud command-line tool.
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Cloud Console, on the project selector page, select or create a Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.
- Enable the Cloud Functions API.
- Install and initialize the Cloud SDK.
- Update
gcloudcomponents:gcloud components update
- Prepare your development environment.
Need a command prompt? You can use the Google Cloud Shell. The Google Cloud Shell is a command line environment that already includes the Google Cloud SDK, so you don't need to install it. The Google Cloud SDK also comes preinstalled on Google Compute Engine Virtual Machines.
Get the sample code
Clone the sample 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.
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/
Take a look at the sample code:
Node.js
Python
Go
Java
Deploy a function
To deploy the function with an HTTP trigger, run the following
command in the helloworld directory:
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:
nodejs10nodejs12
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:
python37python38
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:
go111go113
Java
gcloud functions deploy java-helloworld \ --entry-point functions.HelloWorld \ --runtime java11 \ --memory 512MB --trigger-http --allow-unauthenticated
The --allow-unauthenticated flag lets you reach the function
without authentication.
To require
authentication, omit the
flag.
Test the function
When the function finishes deploying, take note of the
httpsTrigger'surlproperty or find it using the following command:Node.js
gcloud functions describe helloGET
Python
gcloud functions describe hello_get
Go
gcloud functions describe HelloGet
Java
gcloud functions describe java-helloworld
It should look like this:
Node.js
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloGET
Python
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_get
Go
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/HelloGet
Java
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/java-helloworld
Visit this URL in your browser. You should see a
Hello World!message.