Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Workspaces vs Directory-Based Separation in Terraform
📖 Scenario: You are managing infrastructure for a company that has multiple environments: development and production. You want to organize your Terraform code to handle these environments safely and efficiently.Two common ways to separate environments are using Terraform workspaces or using directory-based separation with different folders for each environment.
🎯 Goal: Build two simple Terraform configurations: one using terraform workspace to separate dev and prod environments, and another using separate directories for dev and prod. You will create a resource in each setup that shows how the environment name is used.
📋 What You'll Learn
Create a Terraform configuration with a variable environment set by workspace
Create a resource that uses the environment variable in its name
Create a separate directory structure for dev and prod with their own terraform.tfvars
Create a resource in each directory that uses the environment variable from terraform.tfvars
💡 Why This Matters
🌍 Real World
Managing multiple environments safely is common in real-world infrastructure projects. Workspaces and directory separation are two practical ways to organize Terraform code for this.
💼 Career
Cloud engineers and DevOps professionals often need to manage infrastructure for multiple environments. Knowing how to separate environments using Terraform workspaces or directories is a key skill.
Progress0 / 4 steps
1
Create Terraform configuration using workspaces
Create a Terraform file named main.tf with a variable called environment that gets its default value from the current workspace using terraform.workspace. Then create an AWS S3 bucket resource named myapp-${var.environment}-bucket using the environment variable.
Terraform
Hint
Use terraform.workspace to get the current workspace name as the default value for the environment variable.
2
Add workspace creation commands and select workspace
Add a Terraform CLI command comment to create two workspaces named dev and prod. Then add a comment showing how to select the dev workspace before applying.
Terraform
Hint
Use terraform workspace new to create workspaces and terraform workspace select to switch.
3
Create directory-based separation with variables and resource
Create two directories named dev and prod. In each directory, create a main.tf file with a variable environment set without a default. Then create an AWS S3 bucket resource named myapp-${var.environment}-bucket. Also create a terraform.tfvars file in each directory setting environment to the directory name.
Terraform
Hint
Each directory has its own main.tf and terraform.tfvars to set the environment variable.
4
Add README comments explaining workspace vs directory separation
Add a README comment explaining that workspaces allow using one codebase with multiple environment states, while directory-based separation uses separate folders and variable files for each environment.
Terraform
Hint
Explain the main difference between workspaces and directory-based separation in simple terms.
Practice
(1/5)
1. What is the main difference between Terraform workspaces and directory-based separation?
easy
A. Workspaces require separate folders; directory-based uses one folder with multiple states.
B. Workspaces store state remotely; directory-based stores state locally only.
C. Workspaces and directory-based separation are exactly the same.
D. Workspaces use one folder with multiple states; directory-based uses separate folders for each environment.
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 D
Quick Check:
Workspaces = one folder, multiple states [OK]
Hint: Workspaces = one folder, directory = multiple folders [OK]
Common Mistakes:
Confusing workspace with directory-based separation
Thinking workspaces require multiple folders
Assuming directory-based uses one state file
2. Which Terraform command correctly switches to a workspace named dev?
easy
A. terraform workspace select dev
B. terraform select workspace dev
C. terraform switch workspace dev
D. terraform workspace change dev
Solution
Step 1: Recall Terraform workspace commands
The correct command to switch workspace is terraform workspace select <name>.
Step 2: Match command to options
Only terraform workspace select dev matches the correct syntax exactly.
Each folder has its own Terraform code and state, so running inside envs/dev affects only dev.
Step 2: Analyze command effect
terraform apply in envs/dev applies changes only to dev environment's resources.
Final Answer:
Terraform applies changes only to the dev environment using its own state. -> Option B
Quick Check:
Directory-based apply affects current folder environment [OK]
Hint: Apply runs in current folder's environment only [OK]
Common Mistakes:
Assuming apply affects all environments
Thinking state is shared across folders
Expecting errors due to missing state
4. You created a new workspace named staging but when running terraform apply, changes apply to the default workspace instead. What is the likely cause?
medium
A. You forgot to run terraform workspace select staging before applying.
B. The staging workspace does not exist.
C. Terraform does not support multiple workspaces.
D. You need to rename the default workspace to staging.
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 run terraform workspace select staging, Terraform stays in default workspace.
Final Answer:
You forgot to run terraform workspace select staging before applying. -> Option A
Quick Check:
Must select workspace before apply [OK]
Hint: Always select workspace before applying changes [OK]
Common Mistakes:
Assuming workspace auto-switches after creation
Thinking workspace names must be renamed
Believing Terraform lacks workspace support
5. You want to manage three environments: dev, staging, and prod. You want to keep code DRY (Don't Repeat Yourself) and share most configuration but keep states isolated. Which approach is best?
hard
A. Use one folder and switch backend configuration files for each environment.
B. Create three separate folders, each with full copies of code and state.
C. Use one folder with Terraform workspaces for each environment.
D. Use one folder and manually rename state files for each environment.
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 C
Quick Check:
Workspaces = shared code, separate states [OK]
Hint: Workspaces share code, separate states for DRY environments [OK]
Common Mistakes:
Duplicating code in multiple folders unnecessarily