· Sourceplane engineering · Infrastructure · 4 min read
Infrastructure as code without the sprawl
Many teams have Terraform that nobody dares to apply. The problem is rarely the tool. It is one huge root module, state nobody trusts, and changes made by hand. A structure that stays manageable.
Infrastructure as code promises that production can be read, reviewed and rebuilt from a repository. In practice many teams end up somewhere less comfortable: a large Terraform directory that was started with good intentions, a plan that shows dozens of unexplained changes, and an unwritten rule that nobody runs apply on a Friday, or at all.
When that happens, the tool is rarely the cause. The usual culprits are structure, state and habits.
One giant root module
The most common problem is putting everything in one place: networking, databases, DNS, queues, every service’s resources, all in a single root module with a single state file.
Every plan then touches everything. It is slow, any unrelated drift shows up in every review, and a mistake in one area can block changes in all the others. Nobody can own it, because it belongs to everyone.
Split by ownership and lifecycle. Each root module should be small enough to understand in one sitting, owned by one team, and change at roughly one rhythm. A sensible starting split:
- Foundations that rarely change: accounts, networks, identity.
- Shared data stores: databases, buckets, caches.
- Per-service infrastructure, kept next to the service that uses it.
- Edge and DNS: domains, certificates, routing.
Each has its own state, its own plan and its own pipeline step, and they reference each other through outputs rather than shared files.
State nobody trusts
State is Terraform’s record of what it manages. When it lives on someone’s laptop, is shared without locking, or has been edited by hand, every plan becomes a guess.
- Remote state with locking, always. Two applies at once should be impossible.
- One state per environment and per root module, so a staging change cannot touch production.
- Access to state is access to secrets. State files often contain sensitive values; protect them like credentials.
Changes made by hand
The quickest way to undermine infrastructure as code is the console. A setting changed during an incident, a resource created to test something, a permission added for a one-off job. Each one is drift: reality and code disagree, and the next plan either reverts the change or reports noise.
- Apply only from the pipeline. People propose changes as code; the pipeline plans them on the pull request and applies them on merge.
- Detect drift on a schedule. A daily plan against each environment reports anything that changed outside code.
- Write back emergency fixes before the incident is closed.
Modules: fewer, smaller, owned
Reusable modules help, up to a point. A module that wraps a single resource with a dozen pass-through variables adds indirection without value. A module that encodes a real decision, such as “this is how we create a database with backups, encryption and monitoring”, is worth having.
Keep the number of shared modules small, version them, give each an owner, and prefer composition in root modules over deep nesting.
Bringing existing infrastructure under control
Most teams are not starting from scratch. A safe order for importing what exists:
- Map it. List what exists in each account and environment, including what was built by hand.
- Start at the edges. DNS records, buckets and queues are low risk and build confidence.
- Import, do not recreate. Bring existing resources into state rather than replacing them, and confirm the plan shows no changes before moving on.
- One environment at a time. Do staging first, learn, then production.
- Turn on drift detection as each area comes under management.
Done this way, the move to code happens without downtime and without a big-bang migration.
Cost and recovery come for free
Once infrastructure is declared, two things get much easier. Tagging every resource with its service and environment turns the cloud bill into a per-service view. And recovery stops being theoretical: if the code can build staging from nothing, it can build production from nothing, and you can prove it with a drill.
How we help. We bring infrastructure under code with your team: structure, state, pipelines, drift detection and a recovery drill. See cloud infrastructure, or talk to an engineer.