Quickstart: Deploy a Cloud Run function using the gcloud CLI
This page shows you how to deploy an HTTP Cloud Run 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com - To set the default project for your Cloud Run service:
Replace PROJECT_ID with the name of the project you created for this quickstart.gcloud config set project PROJECT_ID
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.
- Review Cloud Run pricing or estimate costs with the pricing calculator.
Required roles
To get the permissions that you need to complete this quickstart, ask your administrator to grant you the following IAM roles:
-
Cloud Run Source Developer (
roles/run.sourceDeveloper) on the project -
Service Account User (
roles/iam.serviceAccountUser) on the service identity
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Grant the Cloud Build service account access to your project
Cloud Build automatically uses the Compute Engine default service account as the default Cloud Build service account to build your source code and Cloud Run resource, unless you override this behavior.
For Cloud Build to build your sources, grant the Cloud Build service
account the Cloud Run
Builder
(roles/run.builder) role on your project:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT_EMAIL_ADDRESS \ --role=roles/run.builder
Replace PROJECT_ID with your Google Cloud
project ID and SERVICE_ACCOUNT_EMAIL_ADDRESS with the
email address of the Cloud Build service account. If you're using the
Compute Engine default service account as the Cloud Build service account, then
use the following format for the service account email address:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
Replace PROJECT_NUMBER with your Google Cloud
project number.
For detailed instructions on how to find your project ID, and project number, see Creating and managing projects.
Granting the Cloud Run builder role 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
helloworldand change directory into it:mkdir helloworld cd helloworldCreate a
package.jsonfile in thehelloworlddirectory to specify Node.js dependencies:Create an
index.jsfile in thehelloworlddirectory with the following Node.js sample:
Python
Create a new directory named
helloworldand change directory into it:mkdir helloworld cd helloworldCreate a
requirements.txtfile in thehelloworlddirectory, to specify Python dependencies:This adds packages needed by the sample.
Create a
main.pyfile in thehelloworlddirectory with the following Python sample:
Go
Create a new directory named
helloworldand change directory into it:mkdir helloworld cd helloworldCreate a
go.modfile to declare the go module:Create an
hello_http.gofile in thehelloworlddirectory with the following Go code sample:
Java
Create a new directory named
helloworldand change directory into it:mkdir helloworld cd helloworldCreate 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.javaUpdate the
HelloWorld.javafile with the following Java code sample:Create a
pom.xmlfile in thehelloworlddirectory, and add the following Java dependencies:
Ruby
Create a new directory named
helloworldand change directory into it:mkdir helloworld cd helloworldCreate a file named
app.rband paste the following code into it:Create a file named
Gemfileand copy the following into it:If you don't have Bundler 2.0 or greater installed, install Bundler.
Generate a
Gemfile.lockfile by running:bundle install
PHP
Create a new directory named
helloworldand change directory into it:mkdir helloworld cd helloworldCreate a file named
index.phpand paste the following code into it:If you aren't using Cloud Shell, create a
composer.jsonfile and paste the following code into it:
.NET
Install .NET SDK.
From the console, create a new empty web project using the dotnet command.
dotnet new web -o helloworld-csharpChange directory to
helloworld-csharp:Replace the sample code in the project file
helloworld-csharp.csprojwith the following:Replace the sample code in
Program.csfile with the following:
Deploy the function
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 run deploy nodejs-http-function \ --source . \ --function helloGET \ --base-image nodejs22 \ --region REGION \ --allow-unauthenticatedReplace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
europe-west1.Python
gcloud run deploy python-http-function \ --source . \ --function hello_get \ --base-image python313 \ --region REGION \ --allow-unauthenticatedReplace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
europe-west1.Go
gcloud run deploy go-http-function \ --source . \ --function HelloGet \ --base-image go125 \ --region REGION \ --allow-unauthenticatedReplace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
europe-west1.Java
Run the following command in the directory that contains the
pom.xmlfile:gcloud run deploy java-http-function \ --source . \ --function functions.HelloWorld \ --base-image java21 \ --region REGION \ --allow-unauthenticatedReplace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
europe-west1.Ruby
gcloud run deploy ruby-http-function \ --source . \ --function hello_get \ --base-image ruby34 \ --region REGION \ --allow-unauthenticatedReplace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
europe-west1.PHP
gcloud run deploy php-http-function \ --source . \ --function helloGet \ --base-image php84 \ --region REGION \ --allow-unauthenticatedReplace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
europe-west1..NET
gcloud run deploy csharp-http-function \ --source . \ --function HelloWorld.Function \ --base-image dotnet8 \ --region REGION \ --allow-unauthenticatedReplace REGION with the Google Cloud region of the service where you want to deploy your function. For example,
europe-west1.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.
Clean up
To avoid additional charges to your Google Cloud account, delete all the resources you deployed with this quickstart.
Delete your repository
Cloud Run doesn't charge you when your deployed service isn't in use. However, you might still be charged for storing the container image in Artifact Registry. To delete Artifact Registry repositories, follow the steps in Delete repositories in the Artifact Registry documentation.
Delete your service
Cloud Run services don't incur costs until they receive requests. To delete your Cloud Run service, follow one of these steps:
Console
To delete a service:
In the Google Cloud console, go to the Cloud Run Services page:
Locate the service you want to delete in the services list, and click its checkbox to select it.
Click Delete. This deletes all revisions of the service.
gcloud
To delete a service, run the following command:
gcloud run services delete SERVICE --region REGION
Replace the following:
- SERVICE: name of your service.
- REGION: Google Cloud region of the service.
Delete your test project
Deleting your Google Cloud project stops billing for all resources in that project. To release all Google Cloud resources in your project, follow these steps:
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
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.
To create triggers with Eventarc, see Create triggers with Eventarc.