The Terraform state layout we use on every new account
One state file per environment is the default, and it is wrong almost immediately. This is the layout we reach for instead, and the reasoning behind each boundary.
State layout is one of those decisions that costs nothing on day one and a great deal in year two. By then there is a single state file holding the VPC, the cluster and forty application resources, every plan takes four minutes, and nobody will run apply without a second person watching.
Two questions decide the boundaries
How often does this change, and how bad is it if an apply goes wrong? Resources that answer differently belong in different state files.
Changes rarely, catastrophic if wrong
- VPC, subnets, transit gateway
- IAM roles and account structure
- DNS zones
- State buckets themselves
Changes often, recoverable if wrong
- Application infrastructure
- Queues, buckets, caches per service
- Autoscaling configuration
- Observability resources
The directory structure
infra/
├── modules/ # versioned, no provider blocks
│ ├── eks/
│ ├── network/
│ └── service-infra/
└── environments/
├── production/
│ ├── 00-network/ # quarterly changes
│ ├── 10-platform/ # cluster, monthly
│ ├── 20-data/ # databases, monthly
│ └── 30-services/ # per-service, daily
└── staging/
└── … same shape, smaller sizes
Passing values between layers
Remote state reads, in one direction only. Some teams prefer data sources that look resources up by tag, which decouples further but makes failures harder to diagnose. We generally start with remote state and move to lookups only where a genuine need appears.
data "terraform_remote_state" "platform" {
backend = "s3"
config = {
bucket = "acme-tfstate-production"
key = "10-platform/terraform.tfstate"
region = "ap-south-1"
}
}
module "checkout" {
source = "../../../modules/service-infra"
version = "2.4.0"
name = "checkout"
cluster_name = data.terraform_remote_state.platform.outputs.cluster_name
oidc_issuer = data.terraform_remote_state.platform.outputs.oidc_issuer
queues = ["orders", "refunds"]
tags = {
Owner = "team-payments"
CostCentre = "payments"
Tier = "1"
}
}
The CI checks that keep it honest
-
fmt and validate on every pull request
Cheap, fast, and removes an entire category of review comment.
-
Plan every affected layer, post as a comment
Reviewers see the plan next to the diff. A plan showing destruction of anything in layers 00 or 10 requires a second approver.
-
Policy check before merge
Conftest or Sentinel rules: mandatory tags, no public ingress without an exemption, no untagged resources.
-
Nightly drift detection
Plan with
-detailed-exitcodeagainst every layer. Exit code 2 opens a ticket automatically.
What this buys, measured on a recent migration.
In eighteen months.