Create and deploy a Cloud Function (1st gen) by using the Google Cloud CLI
This page shows you how to create and deploy a 1st gen Cloud Function using the Google Cloud CLI.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
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 check if billing is enabled on a project.
-
Enable the Cloud Functions and Cloud Build APIs.
-
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 check if billing is enabled on a project.
-
Enable the Cloud Functions and Cloud Build APIs.
- Install and initialize the gcloud CLI.
- Update
gcloud
components: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 CLI, so you don't need to install it. The Google Cloud CLI 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.
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.
PHP
git clone https://github.com/GoogleCloudPlatform/php-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/get/
PHP
cd php-docs-samples/functions/helloworld_get/
Take a look at the sample code:
Node.js
Python
Go
Java
C#
Ruby
PHP
Deploy a function
To deploy the function with an HTTP trigger, run the following command in the directory containing your function:
Node.js
gcloud functions deploy helloGET \ --runtime nodejs18 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Node.js version:
nodejs18
(recommended)nodejs16
nodejs14
nodejs12
nodejs10
Python
gcloud functions deploy hello_get \ --runtime python311 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Python version:
python311
(recommended)python310
python39
python38
python37
Go
gcloud functions deploy HelloGet \ --runtime go120 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Go version:
go120
(recommended)go119
go118
go116
go113
Java
gcloud functions deploy java-helloworld \ --entry-point functions.HelloWorld \ --runtime java17 \ --memory 512MB --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Java version:
java17
(recommended)java11
C#
gcloud functions deploy csharp-helloworld \ --entry-point HelloWorld.Function \ --runtime dotnet6 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred .NET version:
dotnet6
(recommended)dotnet3
Ruby
gcloud functions deploy hello_get --runtime ruby30 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred Ruby version:
ruby30
(recommended)ruby32
(preview)ruby27
ruby26
PHP
gcloud functions deploy helloGet --runtime php81 --trigger-http --allow-unauthenticatedYou can use the following values for the
--runtime
flag to specify your preferred PHP version:
php81
(recommended)php74
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
'surl
property 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
C#
gcloud functions describe csharp-helloworld
Ruby
gcloud functions describe hello_get
PHP
gcloud functions describe helloGet
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
C#
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/csharp-helloworld
Ruby
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_get
PHP
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloGet
Visit this URL in your browser. You should see a
Hello World!
message.
Delete the function
To delete the function, 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-helloworld
C#
gcloud functions delete csharp-helloworld
Ruby
gcloud functions delete hello_get
PHP
gcloud functions delete helloGet
What's next
See the relevant Your First Function guide for the runtime of your choice to learn how to set up your development environment, create a new function from scratch, specify dependencies, deploy your function, test your function, and view logs. Note that these guides are for Cloud Functions (1st gen) only: