Bird
Raised Fist0
Terraformcloud~5 mins

When workspaces are appropriate in Terraform - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

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
Introduction
Sometimes you want to use the same infrastructure setup but keep different versions separate, like for testing and production. Terraform workspaces help you do this by letting you switch between different copies of your infrastructure state easily.
When you want to manage separate environments like development, staging, and production using the same Terraform code.
When you need to test changes in a safe environment without affecting the live setup.
When you want to keep infrastructure states isolated but avoid duplicating code.
When multiple teams share the same Terraform configuration but manage their own resources separately.
When you want to quickly switch between different infrastructure setups without reconfiguring everything.
Commands
This command shows all the workspaces available in your current Terraform project. It helps you see which environments you can switch to.
Terminal
terraform workspace list
Expected OutputExpected
* default dev prod
This creates a new workspace called 'dev'. You use this to start managing a separate environment without affecting others.
Terminal
terraform workspace new dev
Expected OutputExpected
Created and switched to workspace "dev". You're now on a new workspace called "dev".
This switches your current workspace to 'dev'. All Terraform commands now apply to this workspace's state.
Terminal
terraform workspace select dev
Expected OutputExpected
Switched to workspace "dev".
This applies your Terraform configuration to the current workspace without asking for confirmation. It creates or updates resources for the selected environment.
Terminal
terraform apply -auto-approve
Expected OutputExpected
Terraform used the selected workspace "dev". Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip interactive approval before applying changes
This shows the name of the workspace you are currently working in. It helps confirm you are in the right environment.
Terminal
terraform workspace show
Expected OutputExpected
dev
Key Concept

If you remember nothing else from this pattern, remember: workspaces let you use the same Terraform code to manage separate environments safely and easily.

Common Mistakes
Not switching to the correct workspace before running terraform apply
Terraform will apply changes to the wrong environment, causing unexpected updates or downtime.
Always run 'terraform workspace select <workspace-name>' before applying changes.
Trying to share resources between workspaces directly
Workspaces keep states isolated, so resources in one workspace are invisible to others, causing confusion.
Manage shared resources outside of workspace-specific configurations or use separate Terraform projects.
Assuming workspaces create completely separate infrastructure setups
Workspaces only separate state files; the configuration code is shared, so changes affect all workspaces unless conditionally handled.
Use variables or conditional logic in your Terraform code to customize resources per workspace.
Summary
Use 'terraform workspace new' to create separate environments like dev or prod.
Switch between environments with 'terraform workspace select' before applying changes.
Check your current workspace with 'terraform workspace show' to avoid mistakes.

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

  1. Step 1: Understand workspace purpose

    Workspaces allow you to keep separate states for different environments using the same Terraform code.
  2. Step 2: Compare options

    Options A, B, and C do not relate to managing environments or state separation.
  3. Final Answer:

    To manage multiple environments like development and production with one configuration -> Option C
  4. 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

  1. Step 1: Recall correct workspace switch syntax

    The correct command to switch workspaces is terraform workspace select [name].
  2. Step 2: Validate options

    Options A, B, and D use incorrect command syntax not recognized by Terraform.
  3. Final Answer:

    terraform workspace select testing -> Option A
  4. Quick Check:

    Switch workspace = terraform workspace select [name] [OK]
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

  1. Step 1: Follow workspace creation and switching

    First, 'dev' workspace is created and switched to. Then 'prod' workspace is created and switched to.
  2. Step 2: Check current workspace

    After switching to 'prod', running 'terraform workspace show' outputs the current workspace name, which is 'prod'.
  3. Final Answer:

    prod -> Option A
  4. 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

  1. Step 1: Understand error cause

    The error means the workspace 'staging' does not exist yet in Terraform state.
  2. Step 2: Create the missing workspace

    Use terraform workspace new staging to create it before switching.
  3. Final Answer:

    Run terraform workspace new staging to create it first -> Option D
  4. 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

  1. Step 1: Identify workspace use for multiple environments

    Workspaces allow managing multiple environment states with one config by switching between them.
  2. 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.
  3. Final Answer:

    Create separate workspaces named dev, test, prod and switch before applying changes -> Option B
  4. Quick Check:

    Separate workspaces = safe multi-env management [OK]
Hint: Use one workspace per environment for safety [OK]
Common Mistakes:
  • Mixing environments in one workspace
  • Duplicating configs unnecessarily
  • Ignoring test environment