AW Dev Rethought

✂️ Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away - Antoine de Saint-Exupéry

Infrastructure Deep Dive: Why Terraform Becomes the Problem at Scale


Terraform’s first six months are some of the best in infrastructure tooling. You describe what you want, run plan, read the diff, run apply, and the cloud matches the file. Changes go through pull requests. Environments become reproducible. The thing that used to live in one engineer’s head and a wiki page is now code.

Then the estate grows. Somewhere past a few thousand resources and a few dozen engineers, the tool that removed your infrastructure problems starts generating its own. Plans take twenty minutes. Applies fail halfway. Nobody dares rename anything. A team exists whose job is, in practice, keeping Terraform working.

Terraform didn’t get worse. The model it’s built on has costs that only show up at size.


The State File Becomes a Monolith

Terraform knows what exists by keeping a state file, and every operation runs through it. Early on that file is small and invisible. At scale it becomes the most contended object in your infrastructure.

Each plan refreshes every resource in the state against the cloud provider’s API. With five thousand resources, that’s thousands of API calls before Terraform has computed a single change, so plans crawl and start hitting provider rate limits. The state lock means one person or pipeline works at a time, so engineers queue behind each other for changes that have nothing to do with one another.

The blast radius grows with it. A state that holds your networking, databases and compute means a typo in a DNS record is evaluated in the same operation that could replace a production database. Every change carries the risk profile of the whole file.


Splitting State Moves the Problem

The standard answer is to split: one state per team, per environment, per layer. Plans get fast again and locks stop colliding.

What you’ve done, though, is create a dependency graph between states, and Terraform doesn’t manage that graph. Within one state it orders operations correctly. Across states, the application layer reads the network layer’s outputs through a remote state lookup, and nothing enforces that the network change was applied first, or tells the downstream states they’re now stale.

Inside a state, Terraform orders your changes. Across states, you do, and nobody wrote the order down.

So ordering moves into pipeline configuration, wrapper tools and people’s memories. A change that touches three layers becomes three applies in a sequence somebody has to know. Much of the tooling built around Terraform exists to manage this second graph that Terraform itself can’t see.


A Plan Is Not a Promise

The plan shows what Terraform intends to do. It can’t tell you the apply will succeed.

Applies fail in the middle all the time: a quota is hit, an API times out, a resource name is taken, a permission is missing on step forty of sixty. There is no rollback. The infrastructure is now half-changed, the state reflects whatever completed, and getting back to a consistent position means fixing the cause and applying again, forward, under pressure.

Drift undermines the plan from the other direction. Someone fixes an incident through the console at 3am. An autoscaler or another tool modifies a resource Terraform thinks it owns. The next plan shows changes nobody on the pull request made, and the reviewer has to work out whether Terraform is about to revert an emergency fix. At small scale drift is an occasional surprise. At large scale some part of the estate is always drifted, and every plan needs interpreting before anyone trusts it.


Refactoring Means State Surgery

In application code, renaming something is free. In Terraform, a resource’s address in the code is its identity in the state. Rename it or move it into a module, and Terraform reads that as “destroy the old one, create a new one.”

For a security group that’s an inconvenience. For a database it’s an outage with data loss. Avoiding it means terraform state mv or moved blocks, and at scale it means coordinating that surgery across every state and workspace the resource appears in.

The well-known trap is count. Resources created from a list are addressed by index, so removing the second item shifts every later one, and Terraform proposes destroying and recreating all of them. Teams learn this once, in production.

The result is structural. Large Terraform codebases stop being refactored because improving the code has become riskier than living with it. The layout chosen in year one is the one you still have in year five.


Modules Become a Platform Nobody Staffed

Shared modules start as plain reuse. One team writes a good VPC module and others adopt it.

Two hundred callers later, that module is an internal product with an API, and nobody planned for it to be one. Every change is potentially a breaking change. Callers pin versions, so a dozen versions stay in use, and rolling out a security fix means opening pull requests against hundreds of repositories. The module either grows a variable for every caller’s special case or stays strict and gets forked.

Provider upgrades add to the load. A major provider version changes resource schemas, and the upgrade has to pass through every module and every state that uses it.

All of this is ordinary library maintenance work. The trouble is that it arrives on infrastructure teams that never budgeted for a library to maintain.


What Scale Actually Requires

None of this is a case for leaving. Most alternatives have the same problems in different places, and a declarative, reviewable record of your infrastructure is still worth a great deal.

It does mean running Terraform at scale as a system you operate. Keep states small and split along lines that match ownership. Make the cross-state ordering explicit in tooling so it doesn’t depend on who remembers it. Detect drift on a schedule, so it surfaces before someone’s unrelated plan trips over it. Treat shared modules as products with owners, versioning policy and a deprecation path. Use for_each with stable keys over count. And decide who is allowed to apply what, because a tool that can replace a database on a one-line diff needs guardrails that don’t rely on careful reading.

At scale, the Terraform code is the easy part. The hard part is everything you have to build around it.

That’s the cost the first six months hide.


50 Machine Learning Algorithms Cheatsheet Most downloaded free PDF 50 Machine Learning Algorithms Cheatsheet Blueprints of Intelligence — 50 ML Algorithms decoded Download free Downloaded 448 times

If this article helped you, you can support my work on AW Dev Rethought.


Rethought Relay:
Link copied!

Enjoyed this post?

Stay in the loop

New posts + weekly digest, straight to your inbox.

or

Create a free account

  • Save posts to your vault
  • Like posts & build history
  • New-post alerts

Comments

Add Your Comment

Comment Added!


Evalora: Collect feedback from your teams & staff
AD

Evalora: Collect feedback from your teams & staff

Evalora lets you build a form, share the link or QR code, and see verified results. For companies, training teams and institutions. Free plan available.

Start free