What if you could manage all your environments without juggling multiple folders and risking costly mistakes?
Workspaces vs directory-based separation in Terraform - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine managing multiple environments like development, testing, and production by copying the same Terraform files into different folders manually.
You have to remember which folder is for what, and update each folder separately when you change something.
This manual folder copying is slow and confusing.
It's easy to make mistakes like updating the wrong folder or forgetting to sync changes.
Also, it wastes space and makes tracking changes harder.
Terraform Workspaces let you use one set of files but keep separate states for each environment.
You switch workspaces to manage different environments without copying files.
This keeps your setup clean, reduces errors, and saves time.
terraform -chdir=dev apply terraform -chdir=prod apply
terraform workspace select dev terraform apply terraform workspace select prod terraform apply
Workspaces enable easy, safe switching between environments using the same code, making infrastructure management smoother and less error-prone.
A team managing a web app uses workspaces to deploy updates first to a test environment, then switch to production without duplicating code or risking mix-ups.
Manual directory copies cause confusion and errors.
Workspaces keep one codebase with separate states per environment.
This approach saves time and reduces mistakes.
Practice
Solution
Step 1: Understand workspace concept
Workspaces allow multiple states inside the same folder by switching context.Step 2: Understand directory-based separation
Directory-based separation uses different folders, each with its own code and state files.Final Answer:
Workspaces use one folder with multiple states; directory-based uses separate folders for each environment. -> Option DQuick Check:
Workspaces = one folder, multiple states [OK]
- Confusing workspace with directory-based separation
- Thinking workspaces require multiple folders
- Assuming directory-based uses one state file
dev?Solution
Step 1: Recall Terraform workspace commands
The correct command to switch workspace isterraform workspace select <name>.Step 2: Match command to options
Only terraform workspace select dev matches the correct syntax exactly.Final Answer:
terraform workspace select dev -> Option AQuick Check:
Switch workspace = terraform workspace select [OK]
- Using incorrect command order
- Using non-existent commands like 'switch' or 'change'
- Confusing workspace commands with other terraform commands
envs/
├─ dev/
│ └─ main.tf
└─ prod/
└─ main.tfand using directory-based separation, what happens if you run
terraform apply inside envs/dev?Solution
Step 1: Understand directory-based separation behavior
Each folder has its own Terraform code and state, so running insideenvs/devaffects only dev.Step 2: Analyze command effect
terraform applyinenvs/devapplies changes only to dev environment's resources.Final Answer:
Terraform applies changes only to the dev environment using its own state. -> Option BQuick Check:
Directory-based apply affects current folder environment [OK]
- Assuming apply affects all environments
- Thinking state is shared across folders
- Expecting errors due to missing state
staging but when running terraform apply, changes apply to the default workspace instead. What is the likely cause?Solution
Step 1: Check workspace usage
Creating a workspace does not switch to it automatically; you must select it explicitly.Step 2: Identify missing command
If you don't runterraform workspace select staging, Terraform stays indefaultworkspace.Final Answer:
You forgot to run terraform workspace select staging before applying. -> Option AQuick Check:
Must select workspace before apply [OK]
- Assuming workspace auto-switches after creation
- Thinking workspace names must be renamed
- Believing Terraform lacks workspace support
Solution
Step 1: Analyze DRY and state isolation needs
Sharing code but isolating state fits well with workspaces, which share code folder but separate states.Step 2: Compare options
Use one folder with Terraform workspaces for each environment uses workspaces to keep one codebase and separate states per environment, avoiding code duplication.Step 3: Evaluate other options
Create three separate folders, each with full copies of code and state duplicates code, violating DRY. Use one folder and manually rename state files for each environment is error-prone and manual. Use one folder and switch backend configuration files for each environment requires backend changes, complex to manage.Final Answer:
Use one folder with Terraform workspaces for each environment. -> Option CQuick Check:
Workspaces = shared code, separate states [OK]
- Duplicating code in multiple folders unnecessarily
- Trying manual state file renaming
- Switching backend configs frequently
