Infrastructure as Code with Terraform on Google Cloud
DevOpsTerraformGoogle CloudIaC

Infrastructure as Code with Terraform on Google Cloud

April 13, 20261 min read~119 words

Why Infrastructure as Code?

Managing infrastructure manually is error-prone and hard to replicate. Terraform lets you define infrastructure in code, enabling version control, peer review, and reproducible environments.

Getting Started

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

provider "google" {
  project = "my-project"
  region  = "asia-southeast1"
}

resource "google_cloud_run_service" "app" {
  name     = "my-app"
  location = "asia-southeast1"
  
  template {
    spec {
      containers {
        image = "gcr.io/my-project/app:latest"
      }
    }
  }
}

State Management

Store Terraform state remotely in Google Cloud Storage for team collaboration:

terraform {
  backend "gcs" {
    bucket = "my-terraform-state"
    prefix = "terraform/state"
  }
}

Best Practices

  • Use modules for reusable components
  • Separate environments with workspaces or directories
  • Always run terraform plan before apply
  • Use remote state with locking

Enjoyed this article?

Share it with your network or explore more posts below.