How do you structure Terraform for dev, staging and prod? Workspaces, directories or Terragrunt?
IntermediateScenarioMulti-env and multi-account
Answer
Situation read → All three work. The choice comes down to how different the environments are and how much isolation you need between their state and credentials.
What I'd do →
- Shared modules contain the logic; environments only call them with different inputs.
- Directory per environment (my default): each has its own backend, state and provider config, so the prod state and role are clearly separate.
infra/
modules/eks/
envs/dev/main.tf # backend key dev/...
envs/staging/main.tf
envs/prod/main.tf
- Workspaces are fine when environments are identical and live in the same account, but it's easy to apply in the wrong workspace, and the backend is shared.
- Terragrunt helps when you have many accounts and regions and want to avoid repeating backend and provider blocks.
- Whatever the layout, CI applies prod with a prod-only role, so a dev pipeline physically can't touch prod.
Why → Separate directories make the environment explicit in the path and in the pipeline, which prevents "wrong workspace" accidents.
Interview tip → Mention the risk of terraform workspace select mistakes; it shows real-world experience.
More on Multi-env and multi-account
Design the AWS account structure for a 50-engineer startup that's growing fast.IntermediateHow do you promote the same build from dev to staging to prod?BeginnerBugs keep passing staging and appearing in production. How do you make staging closer to prod?IntermediateDevelopers want a preview environment for every pull request. How would you build it?AdvancedHelm values files or Kustomize overlays for per-environment config?BeginnerCI runs in a shared tools account and must deploy to dev, staging and prod accounts. How do you set up access?Intermediate