Prompt
How do I manage Terraform state for many environments?
Latest observation
A good rule of thumb: keep one state file per “blast radius”, not one huge state for everything.
Common patterns
1) Separate state per environment
Use different state backends/keys for dev, staging, prod.
Pros
- Strong isolation
- Safer prod changes
- Easier to destroy/recreate lower environments
Cons
- More config to manage
- Cross-environment references are harder
Typical setup:
envs/dev/terraform.tfvarsenvs/staging/terraform.tfvarsenvs/prod/terraform.tfvars- backend key like:
state/dev/network.tfstatestate/prod/network.tfstate
2) Separate state per component/module
Split by logical pieces:
- networking
- database
- app
- observability
Then each environment has its own copy of those states.
Pros
- Smaller states
- Faster plans/applies
- Limits impact of changes
Cons
- Need a way to pass outputs between states
This is usually the best balance for larger setups.
3) Workspaces
Terraform workspaces let you use the same config with multiple state instances.
Pros
- Simple for small setups
- Quick to start
Cons
- Easy to misuse
- Not great for production separation
- People often mix env-specific values into the wrong place
For many teams, workspaces are okay for ephemeral dev/test, but separate directories/backends are usually better for real environments.
Recommended approach
For most teams:
- Use separate folders per environment
- Use separate state files per component
- Use a remote backend with locking
- Keep shared modules in reusable module directories
Example structure:
infra/
modules/
vpc/
app/
db/
envs/
dev/
network/
app/
staging/
network/
app/
prod/
network/
app/
Each env/component folder has its own:
- provider config
- backend config
- variables file
- state file
Backend options
Use a remote backend if possible:
- AWS S3 + DynamoDB lock
- Terraform Cloud
- Azure Blob Storage
- GCS + locking solution
Remote state gives you:
- collaboration
- locking
- version history
- safer recovery
Sharing data between states
If app needs VPC info from network, use one of these:
Option A: terraform_remote_state
Read outputs from another state.
data "terraform_remote_state" "network" {
backend = "s3"
config = {
bucket = "my-tf-state"
key = "prod/network.tfstate"
region = "us-east-1"
}
}
Use outputs from that state:
vpc_id = data.terraform_remote_state.network.outputs.vpc_id
Option B: Publish shared values elsewhere
For example:
- AWS SSM Parameter Store
- Consul
- DNS
- tags/naming conventions
This is often better when multiple stacks need the same values.
Best practices
- Never store state in Git
- Lock state during apply
- Keep prod separate from non-prod
- Use naming conventions consistently
- Minimize cross-state dependencies
- Export only necessary outputs
- Avoid giant monolithic state files
- Use CI/CD for applies
- Restrict access to prod state
When to choose what
- Small team / one environment: single state may be fine
- Multiple environments: separate state per env
- Large infra: separate state per env + per component
- Highly dynamic ephemeral envs: workspaces can be okay
- Production-critical systems: avoid workspaces, use explicit dirs/backends
If you want, I can suggest a concrete folder layout and backend setup for AWS, Azure, or GCP.