Terraform on GCP is the de facto Infrastructure as Code (IaC) standard for production GCP environments. This article covers provider setup, state management, Workload Identity Federation, module design, GitHub Actions integration, and cost estimation.
# main.tf
terraform {
required_version = ">= 1.6.0"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 6.0"
}
google-beta = {
source = "hashicorp/google-beta"
version = "~> 6.0"
}
}
backend "gcs" {
bucket = "tfstate-PROJECT"
prefix = "envs/prod"
}
}
provider "google" {
project = "my-project"
region = "asia-northeast1"
}
provider "google-beta" {
project = "my-project"
region = "asia-northeast1"
}# VPC
resource "google_compute_network" "vpc" {
name = "main-vpc"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnet" {
name = "main-subnet"
network = google_compute_network.vpc.id
ip_cidr_range = "10.0.0.0/24"
region = "asia-northeast1"
private_ip_google_access = true
}
# GKE Autopilot
resource "google_container_cluster" "primary" {
provider = google-beta
name = "primary-cluster"
location = "asia-northeast1"
enable_autopilot = true
network = google_compute_network.vpc.id
subnetwork = google_compute_subnetwork.subnet.id
release_channel { channel = "STABLE" }
}
# Cloud Run
resource "google_cloud_run_v2_service" "app" {
name = "my-app"
location = "asia-northeast1"
template {
containers {
image = "asia-northeast1-docker.pkg.dev/PROJECT/repo/app:v1"
resources {
limits = { cpu = "1", memory = "512Mi" }
}
}
}
}
# BigQuery Dataset
resource "google_bigquery_dataset" "main" {
dataset_id = "analytics"
location = "asia-northeast1"
description = "Main analytics dataset"
labels = { env = "prod", team = "data" }
}# Create the state bucket up front (once, by hand) gcloud storage buckets create gs://tfstate-PROJECT \ --location=asia-northeast1 \ --uniform-bucket-level-access gcloud storage buckets update gs://tfstate-PROJECT --versioning # CMEK encryption recommended gcloud storage buckets update gs://tfstate-PROJECT \ --default-encryption-key=projects/PROJECT/locations/.../cryptoKeys/tfstate # Run terraform init after configuring the backend terraform init
# modules/vpc/main.tf
variable "name" { type = string }
variable "cidr" { type = string }
resource "google_compute_network" "vpc" {
name = var.name
auto_create_subnetworks = false
}
# Consumer side
module "vpc" {
source = "./modules/vpc"
name = "prod-vpc"
cidr = "10.0.0.0/16"
}
# Use the official modules
module "gke" {
source = "terraform-google-modules/kubernetes-engine/google//modules/beta-autopilot-private-cluster"
version = "~> 31.0"
project_id = var.project_id
name = "my-cluster"
region = "asia-northeast1"
network = module.vpc.network_name
subnetwork = module.vpc.subnet_name
ip_range_pods = "pods"
ip_range_services = "services"
}terraform/
├── modules/ # reusable modules
│ ├── vpc/
│ ├── gke/
│ └── cloudsql/
├── envs/
│ ├── dev/
│ │ ├── main.tf
│ │ └── terraform.tfvars
│ ├── staging/
│ └── prod/
└── global/ # shared across environments (IAM / Org Policy)
└── main.tf# .github/workflows/terraform.yml
permissions:
contents: read
id-token: write # required to obtain the OIDC token
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: google-github-actions/auth@v2
with:
workload_identity_provider: projects/123/locations/global/workloadIdentityPools/github/providers/repo
service_account: [email protected]
- uses: hashicorp/setup-terraform@v3
- run: terraform init
- run: terraform plan
- run: terraform apply -auto-approve
if: github.ref == 'refs/heads/main'gcloud iam workload-identity-pools create github --location=global gcloud iam workload-identity-pools providers create-oidc repo \ --location=global --workload-identity-pool=github \ --issuer-uri="https://token.actions.githubusercontent.com" \ --attribute-mapping="google.subject=assertion.sub,attribute.repo=assertion.repository" gcloud iam service-accounts add-iam-policy-binding [email protected] \ --role=roles/iam.workloadIdentityUser \ --member="principalSet://iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/github/attribute.repo/owner/repo"
# GitHub Action
- uses: infracost/actions/setup@v3
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}
- run: infracost breakdown --path=. --format=json --out-file=infracost.json
- uses: infracost/actions/comment@v3
with:
path: infracost.jsonWhy should I use Terraform on GCP?
It is the de facto Infrastructure as Code (IaC) tool for production environments, giving you version control, code review, reproducibility, and multi-environment management. OpenTofu (OSS) is a drop-in compatible alternative.
What is the difference between the Google Cloud Provider and the Beta Provider?
google-beta includes new features in Preview. Example: google_compute_instance (GA) vs google-beta_compute_instance. You can mix both in the same configuration.
How should I manage state?
Use a Cloud Storage bucket for remote state — required for team sharing and locking. Configure backend "gcs" and rely on bucket Versioning plus the built-in State Lock support of the GCS backend.
How do I use Workload Identity Federation?
Authenticate Terraform runs from GitHub Actions or GitLab CI without long-lived service account keys. Configure a Pool and Provider, then use Service Account Impersonation for safe access.
What is Config Connector?
A Kubernetes Operator that manages GCP resources via Kubernetes manifests. It integrates cleanly with GitOps and ArgoCD and is one alternative to Terraform.
What about Deployment Manager?
Google's native IaC tool, but it is being deprecated. Migrate to Terraform or Config Connector instead.
How should I design modules?
Split into reusable units (VPC, GKE, Cloud SQL). Use the official modules from the Terraform Registry, and share in-house modules via GitHub or GCS.
How do I estimate cost?
Infracost (OSS) displays cost estimates on every PR. Terraform Cloud, Pulumi, and Spacelift also have built-in cost features.
Related Articles - IaC / DevOps
GitHub Actions + GCP CI/CD: WIF Setup (2026)
GitHub Actions to GCP via Workload Identity Federation — setup, common deploy patterns.
Professional Cloud Developer (PCD): Complete Guide (2026)
Pass the PCD exam — Cloud Run, GKE, App Engine, Cloud SQL/Spanner. The developer-focused Professional cert.
Google Cloud IAM Complete Guide: Roles, Policies, Bindings (2026)
IAM fundamentals — principals, roles, conditions, allow/deny policies. The control plane for every GCP exam.
Cloud Build Complete Guide: Triggers, Steps, Substitutions (2026)
Cloud Build — triggers, build steps, common CI patterns. The build engine of choice on GCP.
* Google Cloud is a trademark of Google LLC. Terraform is a registered trademark of HashiCorp. For the latest information, see the official Google Provider docs.
Practice with certification-focused question sets
View GCP exam prepNicheeLab Editorial Team
NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.
Google Cloud Certification Roadmap (2026)
Choose your GCP certification path — Foundational, Associate...
CDL Cloud Digital Leader: Complete Exam Guide (2026)
Pass the Cloud Digital Leader exam — cloud business value, G...
GAIL Generative AI Leader: Complete Exam Guide (2026)
Pass the Generative AI Leader exam — Gemini, Vertex AI, Work...
Vertex AI Fundamentals for GCP Certs (2026)
Vertex AI basics every cert candidate needs — Workbench, Pip...
Associate Cloud Engineer (ACE): Complete Guide (2026)
Pass the Associate Cloud Engineer exam — Console, gcloud, pr...