Terraform on GCP は本番 GCP 環境の Infrastructure as Code (IaC) のデファクト標準です。 本記事では Provider 設定、State 管理、Workload Identity Federation、Module 設計、GitHub Actions 連携、コスト見積もりまで網羅します。
# 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.jsonGCP で Terraform を使うべき理由は?
本番環境の Infrastructure as Code (IaC) のデファクト。バージョン管理、レビュー、再現性、マルチ環境管理が可能。OpenTofu (OSS) も互換利用可。
Google Cloud Provider と Beta Provider の違いは?
google-beta は新機能 (Preview) を含む。Resource 例: <code>google_compute_instance</code> (GA) vs <code>google-beta_compute_instance</code>。同一構成で混在可能。
State 管理はどうする?
Remote State として Cloud Storage Bucket 必須 (チーム共有 + Lock)。<code>backend "gcs"</code> を使用、Versioning + State Lock (gcs バックエンド標準) で安全。
Workload Identity Federation の使い方は?
GitHub Actions / GitLab CI から Terraform 実行時に SA キー不要で認証。Pool + Provider 設定 → Service Account Impersonation で安全。
Config Connector とは?
Kubernetes Operator として GCP リソースを Kubernetes Manifest で管理。GitOps + ArgoCD 統合が容易。Terraform 代替の一選択肢。
Deployment Manager は?
Google 純正だが廃止予定。Terraform / Config Connector へ移行推奨。
Module はどう設計する?
再利用可能な単位 (VPC / GKE / Cloud SQL) で分割。Terraform Registry の公式 Module を活用、自社 Module は GitHub / GCS で共有。
Cost Estimation はどうする?
Infracost (OSS) で PR ごとにコスト見積もり表示。Terraform Cloud / Pulumi / Spacelift もコスト機能内蔵。
関連記事・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 は Google LLC、Terraform は HashiCorp の登録商標です。最新情報は Google Provider 公式 をご確認ください。
NicheeLab編集部
データエンジニアリング・クラウド資格の専門家。Databricks・Snowflake等の認定資格を保有し、実務経験に基づいた問題作成・解説を行っています。NicheeLab運営。
Google Cloud (GCP) 認定資格ロードマップ 2026 完全版|全 15 試験を体系化
Google Cloud 認定資格 全 15 試験 (Foundational 2 + Associate 3 + Pr...
Cloud Digital Leader (CDL) 完全ガイド|出題範囲・学習リソース・合格戦略
Google Cloud Cloud Digital Leader (CDL) の完全ガイド。6 ドメイン 92 bul...
Generative AI Leader (GAIL) 完全ガイド|Google Cloud 生成 AI 認定
Google Cloud Generative AI Leader (GAIL、2025-05-14 リリース) の完全...
Vertex AI 入門|Google Cloud 統合 ML プラットフォームの全機能
Google Cloud Vertex AI の入門解説。Vertex AI Studio / Agent Builde...
GCP Associate Cloud Engineer (ACE) 完全ガイド|試験範囲・受験料・学習ロードマップ
Google Cloud Associate Cloud Engineer (ACE) の試験範囲・受験料 125 US...