How do Terraform workspaces work and when should you use them?
Learn how Terraform workspaces isolate state, how terraform.workspace works, when to use them, and why teams separate prod and staging differently.
Expected Interview Answer
Terraform workspaces let a single configuration maintain multiple independent state files, so the same code can manage several parallel instances of infrastructure without them overwriting each other.
Each workspace stores its own state under a distinct key in the backend, and you switch between them with terraform workspace select. The current workspace name is available as terraform.workspace, which you can use to vary counts, names, or sizes. They suit ephemeral or per-developer copies of the same environment, but for strongly separated environments like prod and staging most teams prefer separate backends or directories because workspaces share the same backend credentials and configuration.
- One codebase manages many parallel instances
- Isolated state per workspace prevents collisions
- terraform.workspace enables environment-aware values
- Fast to spin up throwaway or per-branch copies
- No duplicate configuration files to maintain
AI Mentor Explanation
Think of one cricket ground hosting several matches on different days using the same pitch and rules. Each match keeps its own scorebook so scores never mix, even though the venue and playing conditions are identical. A workspace is that separate scorebook: identical infrastructure code, but its own state recording exactly what exists for that particular run.
Step-by-Step Explanation
Step 1
Understand the default workspace
Every configuration starts in a workspace named 'default'; you are always in some workspace even if you never switch.
Step 2
Create a new workspace
Run terraform workspace new dev to create an isolated state and switch to it automatically.
Step 3
Switch between workspaces
Use terraform workspace select dev or terraform workspace list to move between and inspect existing workspaces.
Step 4
Reference the workspace in code
Read terraform.workspace to vary resource names, counts, or instance sizes per workspace.
Step 5
Apply per workspace
Each terraform apply only affects the currently selected workspace's state, keeping instances independent.
What Interviewer Expects
- Knowing workspaces isolate state, not code
- Awareness of the default workspace
- Using terraform.workspace for environment-aware values
- When workspaces fit and when separate backends are better
- Understanding the shared-backend limitation
Common Mistakes
- Thinking workspaces create separate configurations or directories
- Using workspaces to separate prod and staging with different credentials
- Forgetting which workspace is selected before applying
- Assuming CLI workspaces and Terraform Cloud workspaces are identical
Best Answer (HR Friendly)
“Terraform workspaces let you reuse the same infrastructure code to run several separate copies of an environment, each with its own record of what exists. They are handy for quick throwaway or per-developer setups, though teams often keep production and staging fully separate instead.”
Code Example
resource "aws_instance" "app" {
ami = "ami-0abcd1234"
instance_type = terraform.workspace == "prod" ? "t3.large" : "t3.micro"
tags = {
Name = "app-${terraform.workspace}"
Environment = terraform.workspace
}
}terraform workspace new dev
terraform workspace list
terraform workspace select prod
terraform applyFollow-up Questions
- Why do many teams avoid workspaces for prod vs staging separation?
- How is terraform.workspace used inside a configuration?
- What is the difference between CLI workspaces and Terraform Cloud workspaces?
- How does state isolation differ between workspaces and separate backends?
- Can you delete a workspace, and what happens to its state?
MCQ Practice
1. What does a Terraform workspace primarily isolate?
Workspaces share the same configuration and backend but keep a separate state file per workspace.
2. Which value exposes the current workspace name in configuration?
terraform.workspace is a built-in expression returning the name of the currently selected workspace.
3. What is the name of the workspace you start in by default?
Every configuration begins in the workspace named 'default' until you create and select another.
Flash Cards
What does a workspace isolate? — The state file — the same code and backend are shared, but each workspace records its own resources.
How do you read the current workspace in code? — Use the built-in terraform.workspace expression.
When are workspaces a poor fit? — For strongly separated environments like prod vs staging needing different credentials or backends.
What is the starting workspace called? — default