How do you manage multiple environments in Terraform?
Answer
Multiple environment strategies in Terraform: Directory-per-environment (recommended): separate directories envs/dev/main.tf, envs/prod/main.tf. Each calls shared modules with different variables. Each environment has its own state file. Strong isolation — a mistake in dev never touches prod state. Workspaces: single config, multiple state files. Simpler but risk of one codebase affecting all environments. Best for short-lived environments. Terragrunt environment hierarchy: parent terragrunt.hcl with DRY backend config; each env folder overrides variables. Scales well for many environments and regions. Variable overrides: use *.auto.tfvars files per environment loaded based on workspace or CLI flags. The directory-per-environment approach is the most robust for production systems because it provides full isolation of state and can use different IAM credentials per environment, preventing accidents.
Previous
What is Atlantis and how does it automate Terraform?
Next
What is Terraform's provider development and custom providers?
More Terraform / IaC Questions
View all →- Intermediate What is the Terraform state locking mechanism?
- Intermediate How do you handle sensitive values in Terraform?
- Intermediate What are Terraform Provisioners and when should you avoid them?
- Intermediate What is Terraform import?
- Intermediate How do you structure Terraform code for large teams?