Getty Images/iStockphoto


Deploy an app on Google Cloud Run with Terraform

Using Terraform to deploy an application to Google Cloud Run enables a scalable deployment process and can reduce configuration struggles. Follow this tutorial to get started.

Pairing Terraform with Cloud Run delivers scalable and predictable deployment. Simultaneously, you can deploy your app in Google Cloud with little configuration hassle, thanks to the deployment process that Cloud Run supports.

Let's look at the steps necessary to deploy an application to Google Cloud Run with Terraform.

What is Google Cloud Run?

Cloud Run is a fully managed service that lets you deploy applications with just a few clicks or CLI commands. Deployment on Cloud Run can look like providing a container image for your app, specifying a few configuration options and telling Cloud Run to run the app. This means you don't have to worry about provisioning host infrastructure or scaling servers.

Cloud Run is notable because it's a container-centric platform. Not all similar services are designed for deploying containerized apps specifically. Cloud Run supports apps written in every language. This is an advantage that you don't get from certain similar services, such as many function as a service (FaaS) platforms that are only compatible with specific programming languages.

What is Terraform?

Terraform is a popular infrastructure as code (IaC) platform. Like other IaC offerings, Terraform lets engineers define how an application should operate using code. Then, they can push that configuration automatically to as many hosting environments as they want.

Terraform makes it easier to deploy applications at scale. It reduces the risk of configuration errors that could arise from human oversight. If the files that define your application deployment are properly coded, your application will act as you need it to.

Deploy to Google Cloud Run with Terraform

Take a look at an example of using Terraform to deploy to Cloud Run.


The only major prerequisite is to have a container image for the app you want to deploy. The container image should be hosted in a container registry that is accessible from the machine that you're using to deploy the app.

Terraform should be installed on the computer you're working from. You can download it from HashiCorp.

Configure Terraform

Step 1. Open a blank file in your favorite text editor and add these lines to start Terraform:


terraform {
  required_version = ">= 1.3"

  required_providers {
	google = ">= 3.3"

This tells Terraform to use version 1.3. At the time of publication, this is the most recent stable release. It also tells it to use version 3.3 or later of the Terraform Google provider; this is important because Terraform introduced support for Cloud Run in this release.

Step 2. Save the file with the name

Step 3. Add these lines to the file:

provider "google" {
  project = "project_id"

Step 4. Replace the value of project_id with the ID of your Google Cloud project. If you haven't yet created a project for deploying your app, you can do so in the Google Cloud console or using the following command:

gcloud projects create "project_id" –name="project_name"

Step 5. Save your file again.

Step 6. Add these lines to your file:

resource "google_cloud_run_service" "run_service" {
  name = "your_app"
  location = "us-europe-west1"

  template {
	spec {
  	containers {
    	image = ""

resource "google_project_service" "run_api" {
  service = ""

  disable_on_destroy = true
   depends_on = [google_project_service.run_api]

Be sure to replace the following values depending on your configuration:

  • Name. Set to the name you want to use for your app.
  • Location. Set to the GCP region you want to use.
  • Image. Should point to the location of the container image for the app you want to deploy.

Step 7. Save your file again.

Deploy to Cloud Run

Now you're ready to deploy the app based on the configuration you defined using Terraform.

Step 1. Test the configuration first by entering the following:

terraform plan

The output summarizes the actions that Terraform will take when you invoke the configuration.

Step 2. Deploy the app by entering the following:

terraform apply

Your app should now be running. By default, it shouldn't be available via public HTTP requests because of access restrictions. You should be able to access it as an authenticated Google Cloud user with the Cloud Run Invoker permission enabled.

You don't need to use Terraform to use Cloud Run or vice versa. You can also deploy apps to Cloud Run using just the Google Cloud CLI. And you can use Terraform to deploy applications to virtually any host environment, not just Cloud Run.

Dig Deeper on Cloud provider platforms and tools

Data Center