Create a Cloud Run function by using the Google Cloud CLI
This page shows you how to create and deploy a Cloud Run 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 Google Cloud project .
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Logging APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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 Google Cloud project .
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Logging APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 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 Run functions sample code:
Node.js
cd nodejs-docs-samples/functions/helloworld/helloworldGet/
Python
cd python-docs-samples/functions/helloworld/
Go
cd golang-samples/functions/functionsv2/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
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 nodejs-http-function \ --gen2 \ --runtime=nodejs20 \ --region=REGION
\ --source=. \ --entry-point=helloGET \
--trigger-http
Use the --runtime
flag to specify the runtime ID of a supported Node.js version
to run
your function.
Python
gcloud functions deploy python-http-function \ --gen2 \ --runtime=python312 \ --region=REGION
\ --source=. \ --entry-point=hello_get \
--trigger-http
Use the --runtime
flag to specify the runtime ID of a supported Python version
to run
your function.
Go
gcloud functions deploy go-http-function \ --gen2 \ --runtime=go121 \ --region=REGION
\ --source=. \ --entry-point=HelloGet \
--trigger-http
Use the --runtime
flag to specify the runtime ID of a supported Go version
to run
your function.
Java
gcloud functions deploy java-http-function \ --gen2 \ --runtime=java17 \ --region=REGION
\ --source=. \ --entry-point=functions.HelloWorld \ --memory=512MB \
--trigger-http
Use the --runtime
flag to specify the runtime ID of a supported Java version
to run
your function.
C#
gcloud functions deploy csharp-http-function \ --gen2 \ --runtime=dotnet6 \ --region=REGION
\ --source=. \ --entry-point=HelloWorld.Function \
--trigger-http
Use the --runtime
flag to specify the runtime ID of a supported .NET version
to run
your function.
Ruby
gcloud functions deploy ruby-http-function \ --gen2 \ --runtime = ruby32 \ --region =REGION
\ --source = . \ --entry-point = hello_get \
- -trigger-http
Use the --runtime
flag to specify the runtime ID of a supported Ruby version
to run
your function.
PHP
gcloud functions deploy php-http-function \ --gen2 \ --runtime = php82 \ --region =REGION
\ --source = . \ --entry-point = helloGet \
- -trigger-http
Use the --runtime
flag to specify the runtime ID of a supported PHP version
to run
your function.
You can optionally use the --allow-unauthenticated
flag to reach the function without authentication
.
This is useful for testing, but we don't recommend using this setting in
production unless you are creating a public API or website. Further, it might
not work for you, depending on your corporate policy settings. See Authenticating for invocation
for
details on how to invoke a function that requires authentication.
Regions
You must supply a region when you deploy a Cloud Run function
. See Locations
for a list of available regions. Your
gcloud CLI configuration has a default region associated with it, but you can
use any supported region you like in the deploy
command.
To see the default region associated with your gcloud CLI configuration , run:
gcloud config list
You can change your default region as follows:
gcloud config set functions/region REGION
Note that even if you are deploying your function to your default region, you
still must include the region in your deploy
command line.
Triggering the function
-
When the function finishes deploying, take note of the
url
property or find it using the following command:Node.js
gcloud functions describe nodejs-http-function --gen2 --region REGION --format = "value ( url ) "
Python
gcloud functions describe python-http-function --gen2 --region REGION --format = "value ( url ) "
Go
gcloud functions describe go-http-function --gen2 --region REGION --format = "value ( url ) "
Java
gcloud functions describe java-http-function --gen2 --region REGION --format = "value ( url ) "
C#
gcloud functions describe csharp-http-function --gen2 --region REGION --format = "value ( url ) "
Ruby
gcloud functions describe ruby-http-function --gen2 --region REGION --format = "value ( url ) "
PHP
gcloud functions describe php-http-function --gen2 --region REGION --format = "value ( url ) "
-
After editing the following command to use your own URI, run it to see a
Hello World!
message:curl -m 70 -X POST URI \ -H "Authorization: Bearer $( gcloud auth print-identity-token ) " \ -H "Content-Type: application/json" \ -d '{}'
Deleting the function
To delete the Cloud Run function you created in this tutorial, run the following command:
Node.js
gcloud functions delete nodejs-http-function --gen2 --region REGION
Python
gcloud functions delete python-http-function --gen2 --region REGION
Go
gcloud functions delete go-http-function --gen2 --region REGION
Java
gcloud functions delete java-http-function --gen2 --region REGION
C#
gcloud functions delete csharp-http-function --gen2 --region REGION
Ruby
gcloud functions delete ruby-http-function --gen2 --region REGION
PHP
gcloud functions delete php-http-function --gen2 --region REGION
You can also delete Cloud Run functions from the Google Cloud console .
What's next
- Learn more about writing Cloud Run functions .
- Learn more about deploying Cloud Run functions .
- Learn more about calling Cloud Run functions .
- Learn more about monitoring Cloud Run functions .
- Learn more about running Cloud Run functions locally .
- Explore the more advanced Cloud Run functions tutorials .