Terraform Advanced — Directory per Environment
Each environment gets its own directory, its own state file, and its own terraform apply.
The Structure
infra-live/
├── modules/ # Shared module source
│ ├── vpc/
│ ├── eks/
│ └── rds/
├── dev/
│ ├── main.tf # module "vpc" { source = "../modules/vpc" }
│ ├── variables.tf
│ └── terraform.tfvars # dev-specific values
├── staging/
│ ├── main.tf
│ ├── variables.tf
│ └── terraform.tfvars
└── prod/
├── main.tf
├── variables.tf
└── terraform.tfvars
Benefits
- Explicit:
cd prod && terraform plan— you know exactly what you’re changing - Isolated: Dev state can never accidentally touch prod
- Reviewable: PR diffs show exactly which environment is affected
- Different configs: Prod can have resources that dev doesn’t (e.g., WAF, CloudWatch alarms)
Drawbacks
- Code duplication: Each environment copies
main.tf(mitigated by shared modules) - Drift between environments: Adding a resource in prod but forgetting staging
- More directories: Repo grows linearly with environments
What’s Next?
Learn the workspace pattern.