Quickstart: Deploy a function to Cloud Run using the gcloud CLI
This page shows you how to use Cloud Run to deploy an HTTP function using the gcloud 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 Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud 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 Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- To set the default project for your Cloud Run service:
gcloud config set project PROJECT_ID
Replace PROJECT_ID with the name of the project you created for this quickstart. If you are under a domain restriction organization policy restricting unauthenticated invocations for your project, you will need to access your deployed service as described under Testing private services.
- For Cloud Build to be able to build your sources, grant the
Cloud Build Service Account
role to the Compute Engine default service account by running the following:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/cloudbuild.builds.builder
Replace
PROJECT_NUMBER
with your Google Cloud project number, andPROJECT_ID
with your Google Cloud project ID. For detailed instructions on how to find your project ID, and project number, see Creating and managing projects.Granting the Cloud Build Service Account role to the Compute Engine default service account takes a couple of minutes to propagate.
Write the sample function
To write an application, follow these steps:
Node.js
Create a new directory named
helloworld
and change directory into it:mkdir helloworld cd helloworld
Create a
package.json
file in thehelloworld
directory to specify Node.js dependencies:Create an
index.js
file in thehelloworld
directory with the following Node.js sample:
Python
Create a new directory named
helloworld
and change directory into it:mkdir helloworld cd helloworld
Create a
requirements.txt
file in thehelloworld
directory, to specify Python dependencies:This adds packages needed by the sample.
Create a
main.py
file in thehelloworld
directory with the following Python sample:
Go
Create a new directory named
helloworld
and change directory into it:mkdir helloworld cd helloworld
Create a
go.mod
file to declare the go module:You can create the
go.mod
file directly in the format as shown, or you can initialize it from the project directory with:go mod init github.com/GoogleCloudPlatform/golang-samples/functions/functionsv2/helloworld/go.mod
Create an
hello_http.go
file in thehelloworld
directory with the following Go code sample:
Java
Create a new directory named
helloworld
and change directory into it:mkdir helloworld cd helloworld
Create the following project structure to contain the source directory and source file:
mkdir -p ~/helloworld/src/main/java/functions touch ~/helloworld/src/main/java/functions/HelloWorld.java
Update the
HelloWorld.java
file with the following Java code sample:Create a
pom.xml
file in thehelloworld
directory, and add the following Java dependencies:
.NET
Install .NET SDK 6.0. This quickstart only works for .NET version 6.
From the console, create a new empty web project using the dotnet command.
dotnet new web -o helloworld-csharp
Change directory to
helloworld-csharp
:Replace the sample code in the project file
helloworld-csharp.csproj
with the following:Replace the sample code in
Program.cs
file with the following:
Deploy the function
Important: This quickstart assumes that you have owner or editor roles in the project you are using for the quickstart. Otherwise, refer to the Cloud Run Source Developer role for the required permissions for deploying a Cloud Run resource from source.
To deploy your Cloud Run function, follow these steps:
Deploy the function by running the following command in the directory that contains the sample code:
Node.js
gcloud beta run deploy nodejs-http-function \ --source . \ --function helloGET \ --base-image nodejs20 \ --region REGION \ --allow-unauthenticated
Replace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
us-central1
.Python
gcloud beta run deploy python-http-function \ --source . \ --function hello_get \ --base-image python312 \ --region REGION \ --allow-unauthenticated
Replace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
us-central1
.Go
gcloud beta run deploy go-http-function \ --source . \ --function HelloGet \ --base-image go122 \ --region REGION \ --allow-unauthenticated
Replace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
us-central1
.Java
Run the following command in the directory that contains the
pom.xml
file:gcloud beta run deploy java-http-function \ --source . \ --function functions.HelloWorld \ --base-image java21 \ --region REGION \ --allow-unauthenticated
Replace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
us-central1
..NET
gcloud beta run deploy csharp-http-function \ --source . \ --function HelloWorld.Function \ --base-image dotnet6 \ --region REGION \ --allow-unauthenticated
Replace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
us-central1
.When the deployment is complete, the Google Cloud CLI displays a URL where the service is running. Open the URL in your browser to see the output of your function.
To learn how to add Eventarc triggers to your function, see the Deploy a function guide for instructions.
Clean up
While Cloud Run does not charge when the service is not in use, you might still be charged for storing the container image in Artifact Registry. You can delete your container image or delete your Google Cloud project to avoid incurring charges. Deleting your Google Cloud project stops billing for all the resources used within that project.
- In the Google 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.
What's next
To deploy a sample function to Cloud Run using the Google Cloud console, see Quickstart: Deploy a function to Cloud Run using the Google Cloud console.
To deploy functions and create triggers using the Google Cloud console and the Google Cloud CLI, see Deploy functions.
To view and delete existing functions, see Manage service revisions.
To build function containers in your own toolchain and deploy it to Cloud Run, see Build functions.