Terraform interview question · Most asked
Workspaces, separate state files, or separate directories: how do you manage multiple environments?
Summary
Terraform CLI workspaces give one configuration several state files (dev, staging, prod) selected at runtime, which is convenient but makes environments look identical and easy to confuse. Many teams prefer separate root modules or directories per environment (each with its own backend key and variables) that call shared modules, or tools such as Terragrunt.
Longer Answer
Separate directories make differences explicit (prod has more replicas, different instance sizes), allow different provider credentials and approval rules per environment, and reduce the risk of applying to prod by mistake. Workspaces suit short-lived copies of the same stack (feature environments). HCP Terraform "workspaces" are a different concept: each is effectively a separate state with its own variables and permissions.
Key Points
- CLI workspaces: one config, many states.
- Separate root modules per environment are explicit.
- Different credentials and approvals per environment.
- HCP workspaces differ from CLI workspaces.
More most-asked Terraform questions
- How do you handle secrets in Terraform?
- How would you run Terraform in CI/CD?
- What is Terraform state, why is it needed, and where should it be stored?
- count or for_each: when do you use each?
- Walk through terraform init, plan and apply. What does each do?
- What is Terraform, and how does it work at a high level?
- What are Terraform modules, and how do you design good ones?
- A terraform plan wants to destroy and recreate a database. What do you do?