Terraform at Scale: Managing Multi-Environment GCP Infrastructure
Infrastructure as Code stopped being optional years ago. If you're still clicking through the GCP console to provision resources, you're creating debt that compounds with every deployment. Nobody remembers which checkbox was ticked in staging, and production drifts a little further away each month.
Terraform fixes that by letting you describe what you want and working out how to get there. The trouble is that the distance between a single main.tf and a setup that safely runs dev, staging and production is large. These are the decisions that matter most when you cross it.
Design modules around services, not resource types
The first decision that matters is module design. I structure Terraform modules around logical service boundaries. A "service" module might include a Cloud Run service, its IAM bindings, a Cloud SQL database, and the networking rules that connect them.
The alternative is a module per resource type: one for buckets, one for service accounts, one for databases. It looks tidy, but it means every new service needs five module calls wired together by hand. Service-shaped modules mirror how teams already think about their systems, so the code reads like an inventory of what you run.
module "billing_api" {
source = "../../modules/service"
name = "billing-api"
region = var.region
image = var.billing_api_image
database_tier = var.database_tier
min_instances = var.min_instances
}
Isolate state for every environment
State management is where most teams stumble. Remote state in a GCS bucket with state locking is table stakes. If two people can run terraform apply against the same state at the same time, you will eventually corrupt it.
You also need state isolation. Dev, staging and production should each have their own state file, so a mistake in dev cannot touch a production resource. I use Terraform workspaces for simple setups and completely separate backend configurations for larger ones.
terraform {
backend "gcs" {
bucket = "acme-terraform-state-prod"
prefix = "billing"
}
}
Separate backends cost a little duplication, and in return the production state lives in a bucket that only the production pipeline can write to. That is a real security boundary, which workspaces alone do not give you.
Keep modules generic with variables and tfvars
Variables and tfvars files handle the differences between environments. The pattern is simple: define variables.tf with sensible defaults, then override per environment with a tfvars file.
# environments/prod/terraform.tfvars
region = "us-central1"
database_tier = "db-custom-2-8192"
min_instances = 2
This keeps the module generic while letting each environment diverge where it needs to. Production gets the bigger database and warm instances. Dev gets the cheapest tier and scales to zero. The module itself never mentions an environment by name.
Run Terraform from CI, without service account keys
The CI/CD integration is critical. Once more than one person changes infrastructure, applying from laptops stops being safe.
I use GitHub Actions with Workload Identity Federation to authenticate to GCP without service account keys. The pipeline does three things:
- Runs
terraform planon every pull request. - Posts the plan as a comment on the pull request.
- Applies only on merge to main.
That gives the team visibility into infrastructure changes before they happen. A reviewer sees "this will destroy and recreate the database" in the pull request, not in an incident channel. I cover the keyless authentication setup in more detail in CI/CD with GitHub Actions: Patterns That Work.
Label everything Terraform manages
One pattern I've found invaluable is tagging every resource with a managed_by = terraform label.
locals {
common_labels = {
managed_by = "terraform"
environment = var.environment
service = var.name
}
}
It makes it trivially easy to find resources that were created manually and need to be imported or removed. A single filter in the console, or one gcloud query for resources missing the label, tells you exactly where the drift is.
Treat Terraform like application code
The biggest lesson: treat your Terraform code with the same rigor as application code. That means:
- Code review on every change, with the plan output in front of the reviewer.
terraform validateandtflintin CI, so broken or sloppy configuration never reaches a plan.- Documentation for each module: what it creates, what it expects, and what it outputs.
None of this is optional once infrastructure is shared. The teams that struggle with Terraform are usually the ones treating it as a pile of scripts instead of a codebase.
Where to start
If you're starting from a console-built project, do it in this order: move state to a locked GCS bucket, split state per environment, wrap one service in a module, then put plan and apply behind pull requests. Each step is useful on its own, and by the last one you'll have infrastructure you can change with confidence.
If the next question is what to run on that infrastructure, I compare the two main options in Kubernetes on GKE: When to Use It and When Cloud Run Is Enough.