google_cloud_run_service

A Cloud Run service has a unique endpoint and autoscales containers.

To get more information about Service, see:

Example Usage - Cloud Run Service Basic

resource "google_cloud_run_service" "default" {
  name     = "cloudrun-srv"
  location = "us-central1"

  template {
    spec {
      containers {
        image = "us-docker.pkg.dev/cloudrun/container/hello"
      }
    }
  }

  traffic {
    percent         = 100
    latest_revision = true
  }
}
Open in Cloud Shell

Example Usage - Cloud Run Service Sql

resource "google_cloud_run_service" "default" {
  name     = "cloudrun-srv"
  location = "us-central1"

  template {
    spec {
      containers {
        image = "us-docker.pkg.dev/cloudrun/container/hello"
      }
    }

    metadata {
      annotations = {
        "autoscaling.knative.dev/maxScale"      = "1000"
        "run.googleapis.com/cloudsql-instances" = google_sql_database_instance.instance.connection_name
        "run.googleapis.com/client-name"        = "terraform"
      }
    }
  }
  autogenerate_revision_name = true
}

resource "google_sql_database_instance" "instance" {
  name             = "cloudrun-sql"
  region           = "us-east1"
  database_version = "MYSQL_5_7"
  settings {
    tier = "db-f1-micro"
  }

  deletion_protection  = "true"
}

Example Usage - Cloud Run Service Noauth

resource "google_cloud_run_service" "default" {
  name     = "cloudrun-srv"
  location = "us-central1"

  template {
    spec {
      containers {
        image = "us-docker.pkg.dev/cloudrun/container/hello"
      }
    }
  }
}

data "google_iam_policy" "noauth" {
  binding {
    role = "roles/run.invoker"
    members = [
      "allUsers",
    ]
  }
}

resource "google_cloud_run_service_iam_policy" "noauth" {
  location    = google_cloud_run_service.default.location
  project     = google_cloud_run_service.default.project
  service     = google_cloud_run_service.default.name

  policy_data = data.google_iam_policy.noauth.policy_data
}

Example Usage - Cloud Run Service Probes

resource "google_cloud_run_service" "default" {
  name     = "cloudrun-srv"
  location = "us-central1"

  template {
    spec {
      containers {
        image = "us-docker.pkg.dev/cloudrun/container/hello"
        startup_probe {
          initial_delay_seconds = 0
          timeout_seconds = 1
          period_seconds = 3
          failure_threshold = 1
          tcp_socket {
            port = 8080
          }
        }
        liveness_probe {
          http_get {
            path = "/"
          }
        }
      }
    }
  }

  traffic {
    percent         = 100
    latest_revision = true
  }

  lifecycle {
    ignore_changes = [
      metadata.0.annotations,
    ]
  }
}

Example Usage - Cloud Run Service Multicontainer

resource "google_cloud_run_service" "default" {
  name     = "cloudrun-srv"
  location = "us-central1"
  provider = google-beta

  metadata {
    annotations = {
      "run.googleapis.com/launch-stage" = "BETA"
    }
  }

  template {
    metadata {
      annotations = {
        "run.googleapis.com/container-dependencies" = jsonencode({hello-1 = ["hello-2"]})
      }
    }
    spec {
      containers {
        name = "hello-1"
        ports {
          container_port = 8080
        }
        image = "us-docker.pkg.dev/cloudrun/container/hello"
        volume_mounts {
          name = "shared-volume"
          mount_path = "/mnt/shared"
          }
      }
      containers {
        name = "hello-2"
        image = "us-docker.pkg.dev/cloudrun/container/hello"
        env {
          name = "PORT"
          value = "8081"
        }
        startup_probe {
          http_get {
            port = 8081
          }
        }
        volume_mounts {
          name = "shared-volume"
          mount_path = "/mnt/shared"
        }
      }
      volumes {
          name = "shared-volume"
          empty_dir {
          medium = "Memory"
          size_limit = "128Mi"
        }
      }
    }
  }

  lifecycle {
    ignore_changes = [
      metadata[0].annotations["run.googleapis.com/launch-stage"],
    ]
  }
}

Argument Reference

The following arguments are supported:

The traffic block supports:

The template block supports:

The metadata block supports:

The spec block supports:

The containers block supports:

The env_from block supports:

The config_map_ref block supports:

The local_object_reference block supports:

The secret_ref block supports:

The local_object_reference block supports:

The env block supports:

The value_from block supports:

The secret_key_ref block supports:

The ports block supports:

The resources block supports:

The volume_mounts block supports:

The startup_probe block supports:

The tcp_socket block supports:

The http_get block supports:

The http_headers block supports:

The grpc block supports:

The liveness_probe block supports:

The http_get block supports:

The http_headers block supports:

The grpc block supports:

The volumes block supports:

The secret block supports:

The items block supports:

The empty_dir block supports:

The csi block supports:


The metadata block supports:

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

The status block contains:

The conditions block contains:

The traffic block contains:

Timeouts

This resource provides the following Timeouts configuration options:

Import

Service can be imported using any of these accepted formats:

In Terraform v1.5.0 and later, use an import block to import Service using one of the formats above. For example:

import {
  id = "locations/{{location}}/namespaces/{{project}}/services/{{name}}"
  to = google_cloud_run_service.default
}

When using the terraform import command, Service can be imported using one of the formats above. For example:

$ terraform import google_cloud_run_service.default locations/{{location}}/namespaces/{{project}}/services/{{name}}
$ terraform import google_cloud_run_service.default {{location}}/{{project}}/{{name}}
$ terraform import google_cloud_run_service.default {{location}}/{{name}}

User Project Overrides

This resource supports User Project Overrides.