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
Using Terraform Workspaces for Environment Management
📖 Scenario: You are managing infrastructure for a web application that needs separate environments for development and production. You want to keep the infrastructure code the same but deploy it differently for each environment without mixing resources.
🎯 Goal: Learn how to use Terraform workspaces to manage multiple environments with the same configuration code.
📋 What You'll Learn
Create a Terraform configuration with a resource
Initialize Terraform workspaces for dev and prod
Use workspace-specific variables
Deploy resources separately in each workspace
💡 Why This Matters
🌍 Real World
Many companies use Terraform workspaces to manage different environments like development, staging, and production without duplicating code.
💼 Career
Understanding workspaces is essential for cloud engineers and DevOps professionals to maintain clean, scalable infrastructure deployments.
Progress0 / 4 steps
1
Create a basic Terraform configuration
Create a Terraform configuration file named main.tf with a resource aws_s3_bucket named my_bucket and set the bucket name to my-app-bucket.
Terraform
Hint
Define the aws_s3_bucket resource with the exact bucket name my-app-bucket.
2
Add workspace-specific bucket name variable
Add a variable bucket_name in variables.tf and update the aws_s3_bucket.my_bucket resource to use var.bucket_name for the bucket name.
Terraform
Hint
Define a variable named bucket_name and use it in the bucket resource.
3
Create workspace-specific variable files
Create two variable files: dev.tfvars with bucket_name = "my-app-bucket-dev" and prod.tfvars with bucket_name = "my-app-bucket-prod".
Terraform
Hint
Create two files named dev.tfvars and prod.tfvars with the exact bucket names.
4
Initialize and use Terraform workspaces
Use Terraform CLI commands to create two workspaces named dev and prod. Then select the dev workspace and apply the configuration using dev.tfvars. Finally, select the prod workspace and apply the configuration using prod.tfvars.
Terraform
Hint
Use the exact Terraform CLI commands to create and switch workspaces and apply configurations with the correct variable files.
Practice
(1/5)
1. What is the main purpose of using terraform workspaces?
easy
A. To store Terraform modules
B. To write Terraform code faster
C. To manage multiple environments like development and production with one configuration
D. To increase the speed of Terraform apply
Solution
Step 1: Understand workspace purpose
Workspaces allow you to keep separate states for different environments using the same Terraform code.
Step 2: Compare options
Options A, B, and C do not relate to managing environments or state separation.
Final Answer:
To manage multiple environments like development and production with one configuration -> Option C
Quick Check:
Workspaces = separate environment states [OK]
Hint: Workspaces separate states for different environments [OK]
Common Mistakes:
Thinking workspaces speed up Terraform runs
Confusing workspaces with modules
Believing workspaces store code
2. Which command correctly switches to a workspace named testing?
easy
A. terraform workspace select testing
B. terraform switch workspace testing
C. terraform workspace switch testing
D. terraform use workspace testing
Solution
Step 1: Recall correct workspace switch syntax
The correct command to switch workspaces is terraform workspace select [name].
Step 2: Validate options
Options A, B, and D use incorrect command syntax not recognized by Terraform.
Hint: Use 'terraform workspace select <name>' to switch [OK]
Common Mistakes:
Using 'switch' instead of 'select'
Mixing command order
Adding extra words like 'workspace' twice
3. Given the commands below, what will be the output of terraform workspace show after these steps?
terraform workspace new dev
terraform workspace select dev
terraform workspace new prod
terraform workspace select prod
terraform workspace show
medium
A. prod
B. dev
C. default
D. Error: workspace not found
Solution
Step 1: Follow workspace creation and switching
First, 'dev' workspace is created and switched to. Then 'prod' workspace is created and switched to.
Step 2: Check current workspace
After switching to 'prod', running 'terraform workspace show' outputs the current workspace name, which is 'prod'.
Final Answer:
prod -> Option A
Quick Check:
Last switched workspace = prod [OK]
Hint: Last 'terraform workspace select' sets current workspace [OK]
Common Mistakes:
Assuming default workspace remains active
Confusing creation with switching
Expecting error without reason
4. You run terraform workspace select staging but get an error saying the workspace does not exist. What should you do to fix this?
medium
A. Rename the workspace to default
B. Run terraform init again
C. Delete the current workspace and try again
D. Run terraform workspace new staging to create it first
Solution
Step 1: Understand error cause
The error means the workspace 'staging' does not exist yet in Terraform state.
Step 2: Create the missing workspace
Use terraform workspace new staging to create it before switching.
Final Answer:
Run terraform workspace new staging to create it first -> Option D
Quick Check:
Workspace must exist before select [OK]
Hint: Create workspace before using it [OK]
Common Mistakes:
Trying to switch without creating workspace
Reinitializing Terraform unnecessarily
Deleting workspaces without cause
5. You want to manage three environments: dev, test, and prod using one Terraform configuration. Which approach best uses workspaces to achieve this safely?
hard
A. Use one workspace and manually change resource names for each environment
B. Create separate workspaces named dev, test, prod and switch before applying changes
C. Create three separate Terraform configurations for each environment
D. Use workspaces only for prod and dev, but not test
Solution
Step 1: Identify workspace use for multiple environments
Workspaces allow managing multiple environment states with one config by switching between them.
Step 2: Evaluate options for safety and simplicity
Create separate workspaces named dev, test, prod and switch before applying changes uses separate workspaces for each environment, which is safe and clean. Use one workspace and manually change resource names for each environment risks conflicts. Create three separate Terraform configurations for each environment duplicates code. Use workspaces only for prod and dev, but not test is inconsistent.
Final Answer:
Create separate workspaces named dev, test, prod and switch before applying changes -> Option B
Quick Check:
Separate workspaces = safe multi-env management [OK]
Hint: Use one workspace per environment for safety [OK]