Terraform at Scale: Lessons from Large Multi-Cloud Estates
Patterns for state decomposition, module governance and drift detection that keep large multi-cloud Terraform estates manageable.
By VVVHQ Team ·
Terraform works well for a few dozen resources and gets harder as the estate grows into hundreds and then thousands, across several accounts, regions and providers. Plans slow down, a change in one place shows up in a diff somewhere else, and nobody is sure which module is the right one. These are the patterns we use to keep large estates manageable. For a regulated medical-device company, for example, we ran 10+ environments across 5+ AWS accounts and 3 regions from one Terraform codebase.
The three problems that hurt large estates
Large state and a large blast radius
One state file for everything means every plan refreshes everything, plans take minutes, and one bad apply can affect the whole estate.
Split the infrastructure into layers that deploy independently: foundation (accounts, VPCs, DNS), platform (Kubernetes clusters, databases), services (per-service resources) and observability. Each layer gets its own state, its own pipeline and its own blast radius. Layers read what they need from the layer below through outputs or data sources.
Splitting an existing monolith is state surgery, and moved and removed blocks let you do it in reviewed pull requests instead of with terraform state commands. On one engagement we split a Terraform monolith into 10 roots and moved 457 resources without destroying one.
Module sprawl
Without shared modules, every team writes its own. You end up with many slightly different ways to create an S3 bucket, and none of them has all the settings production needs.
Build a small library of tested modules that encode your standards (encryption, logging, tags, naming) and publish it with semantic versions in a private registry. Teams then get infrastructure that meets the standards by default, and a fix to a module reaches everyone through a version bump.
Drift and configuration debt
Console changes, emergency fixes that never make it back into code, and scripts that edit resources all widen the gap between Terraform and reality.
Run terraform plan -detailed-exitcode on a schedule for every root, and treat exit code 2 as drift. File each drift finding as a ticket for the team that owns the root, and decide whether to codify the change or revert it. Our post on Terraform drift detection in CI/CD covers the setup.
Patterns that scale
Separate roots for environments
CLI workspaces are convenient for short-lived copies of the same configuration, such as a test environment per branch. HashiCorp's own documentation says they are not suitable when deployments need separate credentials and access controls, which is the case for production. Give production its own root configuration or backend, its own credentials and its own pipeline, and keep the shared logic in modules.
Plan and apply through pull requests
Every change goes through a pull request that shows the plan before anyone approves it. Our typical setup has Atlantis running plan and apply on every repository, with production credentials available only to the pipeline. HCP Terraform and Spacelift do the same as managed services.
Policy as code
At scale, reviewers miss things. Open Policy Agent policies run against each plan and enforce the rules every change must follow:
- No public S3 buckets.
- Encryption on every RDS instance.
- Instance types from an approved list.
- Required tags on every resource.
Cost in the pull request
Infracost adds the estimated monthly cost change to each pull request, so engineers see the cost of a change before they merge it.
What to measure
Track a few numbers per root and per team, and set targets from your own baseline:
- Plan duration for each layer.
- Drift findings per month, and how long they stay open.
- The share of resources created through the shared modules.
- Time to provision a new environment.
- Failed applies as a share of all applies.
Where to start with an existing estate
If the estate is already large and ungoverned, do not try to fix everything at once:
- Inventory every state file, its size and its owner.
- Move the layer that changes most often into its own state.
- Build one shared module, for the resource type your teams create most.
- Add drift detection, even if it is a weekly scheduled plan to begin with.
If your Terraform estate has outgrown the way it was set up, describe it to us and we will say what we would split first.