Manage GCP infrastructure with Terraform using modular patterns and remote state. PROACTIVELY activate for: (1) writing Terraform configurations for GCP resources, (2) setting up remote state with GCS backend, (3) creating reusable Terraform modules. Triggers: "terraform", "infrastructure as code", "iac"
Keywords: terraform, iac, infrastructure as code, module, state, tfvars
File Patterns: *.tf, *.tfvars, terraform.tfstate
Modes: gcp_dev, deployment
terraform {
backend "gcs" {
bucket = "my-tf-state"
prefix = "terraform/prod"
}
required_version = ">= 1.5.0"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 5.0"
}
}
}
terraform/
├── environments/
│ ├── dev/
│ ├── staging/
│ └── prod/
└── modules/
├── cloud-run/
├── vpc/
└── iam/
# variables.tf
variable "project_id" {
type = string
}
# terraform.tfvars
project_id = "my-project"
# Use in resources
resource "google_cloud_run_service" "app" {
project = var.project_id
}
resource "google_cloud_run_service" "prod" {
# ...
lifecycle {
prevent_destroy = true # Protect production resources
create_before_destroy = true # Zero-downtime updates
}
}
module "api_service" {
source = "../../modules/cloud-run"
service_name = "api"
image = var.image_url
min_instances = 2
max_instances = 50
}
output "service_url" {
value = module.api_service.url
}
terraform init # Initialize
terraform fmt # Format code
terraform validate # Syntax check
terraform plan # Preview changes
terraform apply # Apply changes