Cloud Run Terraform module


If you’re a developer who’s looking to deploy containerized applications quickly and easily, you may have heard of Google Cloud Run. I recently had the opportunity to work with Cloud Run and was so impressed by its scalability, portability, and ease of deployment that I decided to create a Terraform Cloud Run module.

locals {
  project        = “your-project-id”
  location       = “us-central1”
  cloud_run_role = “roles/run.invoker”
module “cloud_run_app1” {
  source = “./module”
  cloud_run = {
    cloud_run_name       = “myweb”
    project              = local.project
    location             = local.location      
    cloud_run_role       = local.cloud_run_role
    min_instances        = “1”
    max_instances        = “3”
    container_port       = 80
    container_image      = “yourimage:latest”
    members              = [“allUsers”]
    service_account_name = “serviceaccount-example@email”

variable “cloud_run” {
    type = object({
        cloud_run_name             = string
        project                    = string
        location                   = string    
        cloud_run_role             = string
        service_account_name       = string
        cloud_run_role             = string
        members                    = list(string)
        min_instances              = optional(string)
        max_instances              = optional(string)
        container_image            = optional(string, “nginx:latest”)
        container_port             = optional(number, 80)

The file is a Terraform configuration file that defines the configuration for a Cloud Run service. At the beginning of the file, local variables are defined to store values that will be used later in the file. These variables can be customized to fit the needs of the user. The file also includes a call to a Cloud Run module that has been previously created. In this case, the path to the module is specified as “./module” .This module is used to define the attributes of the Cloud Run service, such as the container image to be deployed, the maximum number of instances to run, the port to listen on etc. Values for these attributes are passed to the module using the var parameter. For example, to set the container image for the Cloud Run service, the container_image variable is passed
to the module with the desired value. Similarly, other attributes can be customized by passing appropriate values to the corresponding variables. Overall, the file provides a convenient way to define the configuration for a Cloud Run service using Terraform, with the flexibility to customize the service as needed.

The file defines the variables that will be used in the Terraform configuration for a Cloud Run service.  One of the variables defined in this file is called cloud_run. This variable has a type of object and contains several attributes that define the configuration for the Cloud Run service.

##### Creating Cloud Run #####

resource “google_cloud_run_service” “cloud_run” {
  name     = var.cloud_run.cloud_run_name
  project  = var.cloud_run.project
  location = var.cloud_run.location

  template {
    spec {
      containers {
        image = var.cloud_run.container_image
        ports {
          container_port = var.cloud_run.container_port
      service_account_name = var.cloud_run.service_account_name

    metadata {
      annotations = {
        “”  = var.cloud_run.min_instances
        “”  = var.cloud_run.max_instances

  traffic {
    percent         = 100
    latest_revision = true

###### Cloud Run IAM Binding #######

resource “google_cloud_run_service_iam_binding” “binding” {
  location = google_cloud_run_service.cloud_run.location
  project  = google_cloud_run_service.cloud_run.project
  service  =
  role     = var.cloud_run.cloud_run_role
  members  = var.cloud_run.members

folder structure

The file is a Terraform configuration file that defines the resources for a Cloud Run service. In this file, a Cloud Run service is created using the google_cloud_run_service. After creating the Cloud Run service, an IAM binding is created using the google_cloud_run_service_iam_binding resource type. This resource type is used to assign an IAM role to the Cloud Run service account, which controls access to the service.

For this project, a custom Docker image is being used to deploy a simple website to a Cloud Run service. This Docker image contains all of the necessary code and dependencies to run the website. In addition to using a custom Docker image, a custom domain can be mapped to the Cloud Run service URL. This can be done by creating a Cloud DNS zone or use your prefered DNS Provider and adding a CNAME record that points to the Cloud Run service URL. This allows the website to be accessed using the custom domain instead of the default Cloud Run URL.