Interview Prep/Real-World Interview/Multi-env and multi-account

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 →

  1. Shared modules contain the logic; environments only call them with different inputs.
  2. 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
  1. 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.
  2. Terragrunt helps when you have many accounts and regions and want to avoid repeating backend and provider blocks.
  3. 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.

All Real-World Interview questions Roadmap: Interview readiness

More on Multi-env and multi-account