Interview › Infrastructure as Code (Terraform, Ansible)
What are Terraform workspaces, and what are their limitations for environments?
Infrastructure as Code (Terraform, Ansible) · Basic level
Answer
Terraform workspaces let the same configuration directory maintain multiple state instances, selected with terraform workspace select. They are useful for lightweight isolation, but they are limited for full environment separation because backend settings, credentials, policies, and directory-level controls often need to differ by environment.
Technical explanation
Workspaces do not automatically isolate IAM permissions, backend configuration, or deployment approvals.
They can be useful for ephemeral review environments when the blast radius is small.
For critical environments, separate roots and state are clearer and easier to govern.
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 are Terraform workspaces, and what are their limitations for environments?
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
- How do you handle resources changed manually outside Terraform?
- What is the difference between using workspaces and separate state files per environment?
- What is configuration drift, and how does Terraform detect it?
- How do you structure Terraform code for multiple environments (dev/staging/prod)?
- What is terraform import, and when do you need it?
- What is the purpose of terraform plan -out and applying a saved plan?
- What is the difference between a data source and a resource?
- What does terraform refresh do, and how has its behaviour changed?