Platform Engineering GitOps & CI/CD 24/7 SRE Support
+91 86790 05791 hello@webkaryon.com Rampur, UP, India · Global Remote
Cloud and cost 4 min read

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/ (repository layout) text
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.

environments/production/30-services/checkout.tf hcl
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

  1. fmt and validate on every pull request

    Cheap, fast, and removes an entire category of review comment.

  2. 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.

  3. Policy check before merge

    Conftest or Sentinel rules: mandatory tags, no public ingress without an exemption, no untagged resources.

  4. Nightly drift detection

    Plan with -detailed-exitcode against every layer. Exit code 2 opens a ticket automatically.

What this buys, measured on a recent migration.

4 min → 25s
median plan time
0
accidental destroys

In eighteen months.

< 15 min
full environment rebuild

Arjun has spent twelve years building delivery systems, most of them on Kubernetes and most of them inherited from someone else. He writes about the parts that are harder than the documentation suggests.

Keep reading

Related field notes