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" }
}# State 用 Bucket 事前作成 (1 回だけ手動) gcloud storage buckets create gs://tfstate-PROJECT \ --location=asia-northeast1 \ --uniform-bucket-level-access gcloud storage buckets update gs://tfstate-PROJECT --versioning # CMEK 暗号化推奨 gcloud storage buckets update gs://tfstate-PROJECT \ --default-encryption-key=projects/PROJECT/locations/.../cryptoKeys/tfstate # Backend 設定後 terraform init で初期化 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
}
# 利用側
module "vpc" {
source = "./modules/vpc"
name = "prod-vpc"
cidr = "10.0.0.0/16"
}
# 公式 Module 活用
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/ # 再利用 Module
│ ├── vpc/
│ ├── gke/
│ └── cloudsql/
├── envs/
│ ├── dev/
│ │ ├── main.tf
│ │ └── terraform.tfvars
│ ├── staging/
│ └── prod/
└── global/ # 全環境共通 (IAM / Org Policy)
└── main.tf# .github/workflows/terraform.yml
permissions:
contents: read
id-token: write # 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: <code>google_compute_instance</code> (GA) vs <code>google-beta_compute_instance</code>. 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 <code>backend "gcs"</code> 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 完全ガイド|Workload Identity・Cloud Run/GKE デプロイ (2026)
GitHub Actions で GCP CI/CD を構築する完全ガイド。Workload Identity Federation、setup-gcloud、Cloud Run / GKE デプロイ、Cloud Build 連携、Reusable Workflow、Self-hosted Runner、Secrets 管理を 2026 年最新版で網羅。
GCP Professional Cloud Developer (PCD) 完全ガイド|Cloud Run・GKE・CI/CD・APM
Google Cloud Professional Cloud Developer の試験範囲、Cloud Run / GKE / Cloud Build / Cloud Trace、AWS DVA / Azure AZ-204 比較、学習ロードマップを徹底解説。
GCP IAM 完全ガイド|Role 体系・Service Account・Workload Identity・Conditions
Google Cloud IAM の全機能を解説。Primitive / Predefined / Custom Role、Service Account、Workload Identity Federation、IAM Conditions、Organization Policy、PAM、Cloud Identity を網羅。
Cloud Build 完全ガイド|CI/CD・cloudbuild.yaml・Private Pool・GitHub 連携 (GCP)
Google Cloud Cloud Build の全機能解説。cloudbuild.yaml、トリガー設定、Private Pool、Workload Identity、Build Approvals、Cloud Deploy 連携、AWS CodeBuild / Azure DevOps 比較を網羅。
* 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...