Quickstart: Deploy a Cloud Run function using the gcloud CLI (original) (raw)

This page shows you how to deploy an HTTP Cloud Run function using the gcloud CLI.

Before you begin

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
    Go to project selector
  3. Make sure that billing is enabled for your Google Cloud project.
  4. Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.
    Enable the APIs
  5. Install the Google Cloud CLI.
  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
  7. To initialize the gcloud CLI, run the following command:
    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
    Go to project selector
  9. Make sure that billing is enabled for your Google Cloud project.
  10. Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.
    Enable the APIs
  11. Install the Google Cloud CLI.
  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
  13. To initialize the gcloud CLI, run the following command:
    gcloud init
  14. 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.
  15. 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.
  16. Grant the Cloud Build service account the following IAM role.

Click to view required roles for the Cloud Build service account

Cloud Build automatically uses theCompute 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, ask your administrator to grantCloud Run Builder (roles/run.builder) to the Compute Engine default service account on your project:
gcloud projects add-iam-policy-binding PROJECT_ID \
--member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
--role=roles/run.builder

Replace PROJECT_NUMBER with your Google Cloud project number, and PROJECT_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 Run builder 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

  1. Create a new directory named helloworld and change directory into it:
   mkdir helloworld  
   cd helloworld  
  1. Create a package.json file in the helloworld directory to specify Node.js dependencies:
  2. Create an index.js file in the helloworld directory with the following Node.js sample:

Python

  1. Create a new directory named helloworld and change directory into it:
   mkdir helloworld  
   cd helloworld  
  1. Create a requirements.txt file in the helloworld directory, to specify Python dependencies:
    This adds packages needed by the sample.
  2. Create a main.py file in the helloworld directory with the following Python sample:

Go

  1. Create a new directory named helloworld and change directory into it:
   mkdir helloworld  
   cd helloworld  
  1. Create a go.mod file to declare the go module:
  2. Create an hello_http.go file in the helloworld directory with the following Go code sample:

Java

  1. Create a new directory named helloworld and change directory into it:
   mkdir helloworld  
   cd helloworld  
  1. 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  
  1. Update the HelloWorld.java file with the following Java code sample:
  2. Create a pom.xml file in the helloworld directory, and add the following Java dependencies:

Ruby

  1. Create a new directory named helloworld and change directory into it:
   mkdir helloworld  
   cd helloworld  
  1. Create a file named app.rb and paste the following code into it:
  2. Create a file named Gemfile and copy the following into it:
  3. If you don't have Bundler 2.0 or greater installed, install Bundler.
  4. Generate a Gemfile.lock file by running:
bundle install  

PHP

  1. Create a new directory named helloworld and change directory into it:
   mkdir helloworld  
   cd helloworld  
  1. Create a file named index.php and paste the following code into it:
  2. If you aren't using Cloud Shell, create a composer.json file and paste the following code into it:

.NET

  1. Install .NET SDK.
  2. From the console, create a new empty web project using the dotnet command.
dotnet new web -o helloworld-csharp  
  1. Change directory to helloworld-csharp:
  2. Replace the sample code in the project file helloworld-csharp.csprojwith the following:
  3. 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:

  1. 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-unauthenticated  

Replace REGION with the Google Cloudregion 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-unauthenticated  

Replace REGION with the Google Cloudregion 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 go123 \  
       --region REGION \  
       --allow-unauthenticated  

Replace REGION with the Google Cloudregion 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.xml file:

gcloud run deploy java-http-function \  
       --source . \  
       --function functions.HelloWorld \  
       --base-image java21 \  
       --region REGION \  
       --allow-unauthenticated  

Replace REGION with the Google Cloudregion 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-unauthenticated  

Replace REGION with the Google Cloudregion 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-unauthenticated  

Replace REGION with the Google Cloudregion 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-unauthenticated  

Replace REGION with the Google Cloudregion of the service where you want to deploy your function. For example, europe-west1. 2. 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

While Cloud Run does not charge when the service is not in use, you might still becharged for storing the container image in Artifact Registry. You can delete your container imageor delete your Google Cloud project to avoid incurring charges. Deleting your Google Cloud project stops billing for all the resources used within that project.

  1. In the Google Cloud console, go to the Manage resources page.
    Go to Manage resources
  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then clickShut down to delete the project.

What's next