Interview › Infrastructure as Code (Terraform, Ansible)
What is the difference between using workspaces and separate state files per environment?
Infrastructure as Code (Terraform, Ansible) · Basic level
Answer
Workspaces multiplex state within one configuration, while separate state files or directories make environment boundaries explicit. For production, I prefer separate state per environment with clear backend keys, permissions, and CI gates. Workspaces are acceptable for simple dev/test variants but can hide risk if prod and dev share too much configuration surface.
Technical explanation
Separate state makes blast radius and access control explicit.
Workspaces can reduce duplication but may encourage one-size-fits-all environment design.
Mature platforms often use modules for reuse and separate roots for isolation.
Keep Terraform's ownership boundary clear: one state should own a resource or field, and other tools should consume published outputs instead of modifying it.
Use fmt, validate, linting, policy checks, plan review, and state locking before production applies.
Design for small blast radius by splitting state around lifecycle, permissions, and recovery boundaries.
Hands-on example
1. Create environment separation for: What is the difference between using workspaces and separate state files per environment?
2. Use reusable modules with separate root modules:
infra/
modules/vpc/
envs/dev/main.tf
envs/stage/main.tf
envs/prod/main.tf
3. Give each environment a separate backend key and credentials boundary:
terraform { backend "s3" { key = "envs/prod/network.tfstate" bucket = "company-tfstate-prod" region = "ap-south-1" use_lockfile = true } }4. Run PR plans for all environments but require manual approval and stricter policy for prod.
5. Use workspaces only for low-risk ephemeral variants after documenting their limitations.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- What are Terraform workspaces, and what are their limitations for environments?
- How do you structure Terraform code for multiple environments (dev/staging/prod)?
- How do you handle resources changed manually outside Terraform?
- What is the purpose of terraform plan -out and applying a saved plan?
- What is configuration drift, and how does Terraform detect it?
- What does terraform refresh do, and how has its behaviour changed?
- What is terraform import, and when do you need it?
- What are provisioners, and why are they considered a last resort?