Infrastructure-as-code patterns that last
Every IaC repo starts clean. Most end up a mess by year 2. Here's what separates the ones that don't.
The patterns that scale
1. Module boundaries match team boundaries
Each team owns their modules. Other teams consume via published versions. No circular dependencies.
2. State per environment, per team
Not one giant state file. Not state per resource. Somewhere in between. Typical: {team}/{environment}/ structure.
3. Variables are inputs, not constants
Anything that differs between environments is a variable. Anything that's identical is hardcoded (for clarity) or in a module (for reuse).
4. Version pinning everywhere
Terraform version. Provider versions. Module versions. Pinned. When you don't pin, your CI/CD becomes a time bomb.
The anti-patterns
The mega-module
One module that does everything. 2,000 lines. Any change breaks unrelated things.
The giant state file
All resources in one Terraform state. Plans take 10+ minutes. Two engineers can't work simultaneously.
The ad-hoc terraform apply
Engineers apply from their laptops. CI/CD applies from somewhere else. State drift. Nobody trusts plans.
The unversioned module
Modules referenced by Git branch. Random change to the module breaks all consumers.
Migration path
From mess to clean:
- Inventory your modules
- Split by team
- Pin versions
- Migrate state file structure
- Move applies to CI/CD only
Takes 3-6 months. Worth it.
Tools
- AI Terraform Generator for common resources.