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
Recall & Review
beginner
What is the main purpose of using workspaces in Terraform?
Workspaces allow you to manage multiple environments (like development, staging, production) using the same configuration but with separate state files. This keeps environments isolated and prevents conflicts.
Click to reveal answer
beginner
How do Terraform workspaces help prevent resource conflicts?
Each workspace has its own state file, so resources created in one workspace do not affect resources in another. This separation avoids accidental changes across environments.
Click to reveal answer
intermediate
Why is it better to use workspaces instead of copying Terraform configurations for each environment?
Using workspaces avoids duplication of code and makes it easier to maintain and update infrastructure because all environments share the same configuration but keep their states separate.
Click to reveal answer
intermediate
What happens if you run Terraform commands without switching to the correct workspace?
Terraform will apply changes to the current workspace's state, which might cause unintended changes in the wrong environment, leading to potential errors or downtime.
Click to reveal answer
advanced
Can Terraform workspaces be used to manage completely different projects?
No, workspaces are designed to manage different environments of the same project. For different projects, separate Terraform configurations and state files are recommended.
Click to reveal answer
What does a Terraform workspace primarily separate?
ATerraform configuration files
BState files for different environments
CCloud provider accounts
DUser access permissions
✗ Incorrect
Workspaces separate state files, allowing multiple environments to use the same configuration without interfering with each other's resources.
Which command switches to a different Terraform workspace?
Aterraform workspace select <name>
Bterraform switch <name>
Cterraform change <name>
Dterraform env <name>
✗ Incorrect
The correct command to switch workspaces is 'terraform workspace select '.
Why should you avoid copying Terraform configs for each environment?
AIt makes maintenance harder and error-prone
BIt disables Terraform commands
CIt increases cloud costs automatically
DIt causes duplicate state files
✗ Incorrect
Copying configs leads to duplicated code, making updates and maintenance more difficult and increasing the chance of mistakes.
What risk exists if you forget to switch to the correct workspace before applying changes?
ANo changes will be applied
BTerraform will crash
CTerraform deletes all resources
DChanges apply to the wrong environment
✗ Incorrect
Applying changes in the wrong workspace can cause unintended modifications in the wrong environment.
Can Terraform workspaces manage multiple unrelated projects?
AYes, they are designed for that
BYes, but only with special plugins
CNo, they are for different environments of the same project
DNo, Terraform does not support multiple projects
✗ Incorrect
Workspaces are intended to separate environments within the same project, not different projects.
Explain how Terraform workspaces help manage multiple environments safely.
Think about how workspaces keep environments from mixing up their resources.
You got /4 concepts.
Describe the risks of not using workspaces or not switching workspaces correctly in Terraform.
Consider what happens if Terraform state files get mixed up.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason Terraform workspaces are used to separate environments?
easy
A. To share the same state file across all environments
B. To keep state files separate for different environments
C. To write different Terraform code for each environment
D. To deploy resources only in the default environment
Solution
Step 1: Understand workspace purpose
Terraform workspaces allow managing multiple environments using the same code but separate state files.
Step 2: Identify how environments stay separate
Each workspace has its own state file, so resources do not mix between environments.
Final Answer:
To keep state files separate for different environments -> Option B
Quick Check:
Separate state files = separate environments [OK]
Hint: Workspaces separate state files, not code [OK]
Common Mistakes:
Thinking workspaces require different code files
Believing all environments share one state file
Assuming workspaces only work for default environment
2. Which command correctly creates a new Terraform workspace named staging?
The correct command to create a workspace is terraform workspace new <name>.
Step 2: Match the command to options
Only terraform workspace new staging matches the correct syntax exactly.
Final Answer:
terraform workspace new staging -> Option C
Quick Check:
Use 'terraform workspace new' to add workspaces [OK]
Hint: Remember: 'terraform workspace new' creates new workspace [OK]
Common Mistakes:
Using 'terraform create workspace' which is invalid
Using 'terraform new workspace' which is not a command
Using 'terraform workspace create' which is invalid
3. Given the following commands run in order:
terraform workspace new dev
terraform workspace select dev
terraform apply
terraform workspace select default
terraform apply
What happens to the resources?
medium
A. Resources are created separately in 'dev' and 'default' environments
B. Resources from 'dev' overwrite those in 'default'
C. Only one set of resources is created in the default workspace
D. Terraform throws an error on switching workspaces
Solution
Step 1: Analyze workspace creation and selection
The 'dev' workspace is created and selected, then resources are applied there.
Step 2: Switch to 'default' workspace and apply again
Switching to 'default' workspace applies resources separately using its own state file.
Final Answer:
Resources are created separately in 'dev' and 'default' environments -> Option A
Quick Check:
Different workspaces = separate resource sets [OK]
Hint: Switching workspaces uses separate states, so resources stay separate [OK]
Common Mistakes:
Assuming resources merge across workspaces
Thinking switching workspaces causes errors
Believing only one workspace can have resources
4. You run terraform workspace select prod but get an error: Workspace 'prod' does not exist. What is the best fix?
medium
A. Run terraform workspace new prod before selecting
B. Edit the Terraform code to add 'prod' workspace
C. Delete the current workspace and retry
D. Run terraform init again
Solution
Step 1: Understand the error meaning
The error means the 'prod' workspace does not exist yet in Terraform.
Step 2: Create the missing workspace
Use terraform workspace new prod to add it before selecting.
Final Answer:
Run terraform workspace new prod before selecting -> Option A
Quick Check:
Create workspace before selecting it [OK]
Hint: Create workspace first, then select it [OK]
Common Mistakes:
Trying to select a workspace that doesn't exist
Editing code instead of managing workspaces
Reinitializing Terraform unnecessarily
5. You want to deploy the same Terraform configuration to dev, staging, and prod environments using workspaces. Which approach best avoids resource conflicts and keeps environments isolated?
hard
A. Deploy all environments in the default workspace with different variable files
B. Use one workspace and manually change resource names for each environment
C. Use different Terraform configuration files for each environment in one workspace
D. Create separate workspaces for each environment and deploy in each workspace
Solution
Step 1: Understand workspace isolation
Each workspace has its own state file, so creating separate workspaces isolates environments safely.
Step 2: Compare options for managing multiple environments
Using separate workspaces avoids manual renaming and code duplication, reducing errors.
Final Answer:
Create separate workspaces for each environment and deploy in each workspace -> Option D
Quick Check:
Separate workspaces = isolated environments [OK]
Hint: Use separate workspaces per environment for clean separation [OK]
Common Mistakes:
Trying to manage all environments in one workspace
Duplicating code instead of using workspaces
Relying on variable files without workspace separation