Bird
Raised Fist0
Terraformcloud~5 mins

Why workspaces separate environments in Terraform - Why It Works

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
When managing infrastructure, you often need separate setups for testing, development, and production. Terraform workspaces help keep these environments separate using the same configuration, avoiding conflicts and mistakes.
When you want to test changes without affecting your live production setup
When you need to maintain multiple versions of infrastructure for different teams
When you want to deploy the same infrastructure in different regions or accounts
When you want to keep your development and production environments isolated
When you want to reuse the same Terraform code but with different settings
Commands
This command creates a new workspace named 'dev' to isolate development environment resources.
Terminal
terraform workspace new dev
Expected OutputExpected
Created and switched to workspace "dev". You are now using workspace "dev".
Lists all existing workspaces and shows which one is currently active.
Terminal
terraform workspace list
Expected OutputExpected
* dev default
Applies the Terraform configuration in the current workspace, creating or updating resources for that environment.
Terminal
terraform apply
Expected OutputExpected
Terraform used the selected workspace "dev". Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Switches back to the 'default' workspace, usually used for production or main environment.
Terminal
terraform workspace select default
Expected OutputExpected
Switched to workspace "default".
Applies the Terraform configuration in the 'default' workspace, managing production environment resources separately from 'dev'.
Terminal
terraform apply
Expected OutputExpected
Terraform used the selected workspace "default". Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Key Concept

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

Common Mistakes
Not switching to the correct workspace before running terraform apply
This causes changes to be applied to the wrong environment, risking production stability or mixing resources.
Always run 'terraform workspace select <name>' to switch to the right environment before applying changes.
Assuming workspaces create completely isolated infrastructure backends
Workspaces share the same backend but separate state files; misunderstanding this can cause confusion about resource isolation.
Understand that workspaces isolate state but not backend configuration; use separate backends if full isolation is needed.
Summary
Create and switch workspaces to separate environments using 'terraform workspace new' and 'terraform workspace select'.
Apply infrastructure changes in the active workspace to keep environments isolated.
List workspaces to see all environments and the current active one.

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

  1. Step 1: Understand workspace purpose

    Terraform workspaces allow managing multiple environments using the same code but separate state files.
  2. Step 2: Identify how environments stay separate

    Each workspace has its own state file, so resources do not mix between environments.
  3. Final Answer:

    To keep state files separate for different environments -> Option B
  4. 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?
easy
A. terraform new workspace staging
B. terraform create workspace staging
C. terraform workspace new staging
D. terraform workspace create staging

Solution

  1. Step 1: Recall Terraform workspace creation syntax

    The correct command to create a workspace is terraform workspace new <name>.
  2. Step 2: Match the command to options

    Only terraform workspace new staging matches the correct syntax exactly.
  3. Final Answer:

    terraform workspace new staging -> Option C
  4. 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

  1. Step 1: Analyze workspace creation and selection

    The 'dev' workspace is created and selected, then resources are applied there.
  2. Step 2: Switch to 'default' workspace and apply again

    Switching to 'default' workspace applies resources separately using its own state file.
  3. Final Answer:

    Resources are created separately in 'dev' and 'default' environments -> Option A
  4. 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

  1. Step 1: Understand the error meaning

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

    Use terraform workspace new prod to add it before selecting.
  3. Final Answer:

    Run terraform workspace new prod before selecting -> Option A
  4. 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

  1. Step 1: Understand workspace isolation

    Each workspace has its own state file, so creating separate workspaces isolates environments safely.
  2. Step 2: Compare options for managing multiple environments

    Using separate workspaces avoids manual renaming and code duplication, reducing errors.
  3. Final Answer:

    Create separate workspaces for each environment and deploy in each workspace -> Option D
  4. 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