Deploy Serverless Containers with Cloud Run and Terraform

Cloud Computing
Date:October 29, 2026
Topic:
Deploy Serverless Containers with Cloud Run and Terraform
⏱ 3 min read

You push code. Google runs it. No clusters to patch, no nodes to size, no 3 AM pages because a node pool drained. That is the promise of Cloud Run, and Terraform makes it repeatable.

Why Serverless Containers Change the Game

Traditional Kubernetes gives you control. It also gives you etcd upgrades, CNI debugging, and autoscaler tuning. Cloud Run abstracts the control plane entirely. You hand it a container image; it serves HTTPS requests and scales to zero when traffic stops. You pay only for vCPU and memory while requests execute.

💡
TipCloud Run supports any language, any binary, any framework that listens on $PORT. No buildpacks required.

Prerequisites

Install the Google Cloud CLI, authenticate with gcloud auth application-default login, and enable the Run, Artifact Registry, and Cloud Build APIs. Terraform 1.6+ and the hashicorp/google provider 5.0+ are required.

hcl
terraform {
  required_version = ">= 1.6"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 5.0"
    }
  }
}

provider "google" {
  project = var.project_id
  region  = var.region
}

Artifact Registry First

Store images in a regional Docker repository. It integrates with Cloud Build and Cloud Run natively, and IAM controls are granular.

hcl
resource "google_artifact_registry_repository" "app_repo" {
  location      = var.region
  repository_id = "app-images"
  description   = "Docker images for Cloud Run services"
  format        = "DOCKER"
}

Build and Push with Cloud Build

Use a cloudbuild.yaml triggered by your VCS. The build step tags the image with the commit SHA for immutable deployments.

yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', '${_REGION}-docker.pkg.dev/$PROJECT_ID/app-images/${_SERVICE}:$COMMIT_SHA', '.']
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', '${_REGION}-docker.pkg.dev/$PROJECT_ID/app-images/${_SERVICE}:$COMMIT_SHA']
images:
- '${_REGION}-docker.pkg.dev/$PROJECT_ID/app-images/${_SERVICE}:$COMMIT_SHA'

Define the Cloud Run Service

The google_cloud_run_v2_service resource replaces the v1 beta. It supports traffic splitting, secrets, VPC connectors, and revision-level rollback.

hcl
resource "google_cloud_run_v2_service" "api" {
  name     = var.service_name
  location = var.region

  template {
    containers {
      image = "${var.region}-docker.pkg.dev/${var.project_id}/app-images/${var.service_name}:${var.image_tag}"
      ports {
        container_port = 8080
      }
      resources {
        limits = {
          cpu    = "1000m"
          memory = "512Mi"
        }
      }
      env {
        name  = "ENV"
        value = "production"
      }
    }
    scaling {
      min_instance_count = 0
      max_instance_count = 100
    }
    vpc_access {
      egress = "ALL_TRAFFIC"
    }
  }

  traffics {
    type         = "TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST"
    percent      = 100
    latest_revision = true
  }
}
ℹ️
NoteSet <code>min_instance_count > 0</code> only if cold starts hurt latency-sensitive paths. Otherwise, keep it at zero to maximize cost savings.

IAM and Public Access

Grant roles/run.invoker to allUsers for public endpoints. For internal services, restrict to specific service accounts or use IAP.

hcl
resource "google_cloud_run_v2_service_iam_member" "public" {
  service  = google_cloud_run_v2_service.api.name
  location = var.region
  role     = "roles/run.invoker"
  member   = "allUsers"
}

Observability Built In

Cloud Run emits request logs, metrics, and traces to Cloud Logging and Cloud Monitoring automatically. Enable cloud_trace_auto_sampling in the container env to get distributed traces without code changes.


✦

Deploy Today

Run terraform init && terraform apply. Push a commit. Watch Cloud Build promote a new revision. Traffic shifts instantly. Roll back with one click in the console or terraform apply -replace. You now own a production-grade serverless pipeline with zero cluster ops.

⚠️
WarningPin the provider version. Google provider upgrades often rename Cloud Run v2 attributes, breaking plans.
Share𝕏 Twitterin LinkedInin Whatsapp