Terraform

Infrastructure as Code

Clicking in the console is not repeatable. Terraform describes infrastructure in code that is reviewed, versioned and applied the same way in every environment.

terraform {
  required_providers {
    google = { source = "hashicorp/google", version = "~> 6.0" }
  }
  backend "gcs" { bucket = "shop-tf-state" }    # shared, locked state
}

provider "google" {
  project = "shop-dev-1234"
  region  = "asia-south1"
}

resource "google_storage_bucket" "media" {
  name                        = "shop-dev-media-1234"
  location                    = "ASIA-SOUTH1"
  uniform_bucket_level_access = true
}

resource "google_cloud_run_v2_service" "api" {
  name     = "course-api"
  location = "asia-south1"
  template {
    containers {
      image = "asia-south1-docker.pkg.dev/shop-dev-1234/apps/course-api:1.2.0"
    }
  }
}

Workflow

Example

terraform init
terraform plan -out plan.tfplan
terraform apply plan.tfplan

Example output

Plan: 2 to add, 0 to change, 0 to destroy.
google_storage_bucket.media: Creating...
google_cloud_run_v2_service.api: Creating...
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Tip: Run terraform plan in pull requests so reviewers see exactly what will change.