Skip to content

Cloud Run

In this section we will create Cloud Run service with Terraform to deploy a "hello world" application.

You will learn how to create your first resource block, reference some variables and grab an output from a resource. In addition we'll be running our first terraform plan's and apply's.

Cloud Run is Google's serverless, Containers-as-a-Service platform.

You can naturally deploy any container image you wish, as long as it serves a HTTP port and otherwise complies with the requirements of Cloud Run.

Exercises

  1. Create a resource block with type google_cloud_run_service and name default with parameters:
    • name = "${var.name}-cloud-run-srv"
    • location = var.region
    • Template:
        template {
          spec {
            containers {
              image = "gcr.io/cloudrun/hello"
            }
          }
        }
      
  2. Create a output block named cloud-run-url with the following parameter:
    • value = google_cloud_run_service.default.status[0].url
    • This can be set in the same cloud_run.tf file or, in the output.tf file!

You are now referencing one of the variables created earlier, with "${var.name}-cloud-run-srv" and retrieving some data from the google_cloud_run_service to be created.

You might recognize that the template parameter block for google_cloud_run_service strangely resembles a Kubernetes manifest. That is not an accident, as Cloud Run is based on Kubernetes. It is just very well hidden inside the service.

You'll also notice, that the syntax for passing in a block to a resource is different from the other values. Note the missing = for example!

Extra

  1. Change the port cloudrun/hello is listening to
    • It reads an environment variable called PORT
    • You will also need to check google_cloud_run_service's documentation so the port matches on the services parameters
  2. Deploy two Cloud Run services with count
    • Remember to change the reference in the output accordingly

Check your configuration

To check your Terraform configuration run:

terraform plan

It will show you the resources that Terraform will create, delete or change. It will not apply anything.

terraform plan is a handy step to see changes which would happen, but it is not sometimes enough to catch all mistakes!

Apply your configuration

Run terraform apply, to make changes to the infrastructure. This will show a similar output to terraform plan above, and then ask you whether to proceed.

terraform apply
Error

That gives an error! Try to fix it by defining the missing parameter.

If you created a data source, fetch the ID with data.google_project.my_project.project or reference the var.project created earlier.

Once you have fixed the resource, you can run terraform apply again. This time everything should work, or you may have to iterate a bit with terraform plan + apply.

In the outputs, the .run.app is now visible. You can try to visit that in a browser. Don't worry about the "Error: Forbidden", we'll fix that next.


Answer

cloud_run.tf

resource "google_cloud_run_service" "default" {
  name = "${var.name}-cloud-run-srv"
  location = var.region

  project = var.project

  template {
    spec {
      containers {
        image = "gcr.io/cloudrun/hello"
      }
    }
  }
}

output.tf

output "cloud-run-url" {
  value = google_cloud_run_service.default.status[0].url
}

Next

Next, we set up some IAM rules.