Skip to content
Rivar.

Draw your Terraform state boundaries before they draw themselves

One state file per environment is the default that turns into a 40-minute plan nobody trusts. A simpler rule for splitting state — and how to get there without a rewrite.

Rivar Technologies3 min read
  • Terraform
  • Infrastructure as Code
  • Platform

Every large Terraform repository we inherit has the same origin story. It started with one state file, because one state file was obviously right for eleven resources. It is now nine hundred resources, the plan takes forty minutes, and there is exactly one engineer willing to run apply.

The failure is not the size of the state. It is that the boundary was never chosen — it was inherited from the first commit.

The rule

Split state along blast radius and ownership, in that order.

Blast radius first: if a mistaken apply in one part of the estate must never be able to destroy another part, they belong in separate states. Terraform's unit of atomicity is the state file, so the state boundary is the blast-radius boundary. Nothing else enforces it.

Ownership second: if two teams change two sets of resources on different schedules, a shared state means a shared lock, which means one team waiting on the other's plan. That is a queue, and queues make people batch changes, and batched changes are the risky ones.

For most estates that produces four tiers:

| Tier | Contents | Changes | | --- | --- | --- | | Foundation | Accounts, organisations, DNS zones, IAM baseline | Rarely, by one team | | Network | VPCs, subnets, transit gateways, peering, endpoints | Occasionally | | Platform | Clusters, databases, shared middleware | Weekly | | Workload | Per-service infrastructure | Continuously, by the owning team |

Foundation gets its own state. Network gets one per region. Platform gets one per environment. Workload gets one per service per environment — which sounds like a lot of state files, and is exactly the point: a workload apply should plan in thirty seconds and be unable to touch anything outside itself.

Wiring the tiers together

Lower tiers publish, higher tiers consume. Publish through a stable, explicit interface — SSM Parameter Store or Azure App Configuration — rather than terraform_remote_state:

# Foundation publishes.
resource "aws_ssm_parameter" "vpc_id" {
  name  = "/platform/${var.env}/network/vpc_id"
  type  = "String"
  value = module.vpc.vpc_id
}

# Workload consumes.
data "aws_ssm_parameter" "vpc_id" {
  name = "/platform/${var.env}/network/vpc_id"
}

terraform_remote_state requires read access to the entire producing state file, including anything sensitive in it, and it couples consumers to the producer's internal structure. A named parameter is a contract. You can also change what is behind it without every consumer noticing.

Getting there without a rewrite

You do not need a big-bang split, and you should not attempt one.

  1. Carve off the leaves first. Workload resources at the edge of the graph have the fewest inbound dependencies. Move one service, verify both states plan clean, repeat.
  2. Use moved blocks and terraform state mv per batch. Small batches. Verify an empty plan after each one before continuing — an empty plan is the only proof the move was lossless.
  3. Publish before you split. Add the SSM parameters while the resources are still in the old state, so consumers can switch to reading them before anything moves underneath.
  4. Leave foundation until last. It is the most dangerous and the least urgent.

A typical estate takes six to ten weeks this way, with the plan time improvement arriving incrementally rather than at the end.

The test

When it is right, an engineer can open a pull request against a workload, read the entire plan without scrolling past what they changed, and merge it on a Friday afternoon without asking anyone.

If that is not true, the boundary is still in the wrong place.

Start here

Tell us what is on fire

Or what is merely smouldering. Describe the situation and we will tell you what we would do about it — including when the answer is that you do not need us.

  • A senior engineer reads it, not a sales team
  • A straight answer on whether we can help
  • No obligation, and no follow-up sequence

Prefer email?

hello@rivar.ca

Optional

The more specific, the more useful our first reply will be.

We reply within one business day. No newsletter, no sales sequence.