Bird
Raised Fist0
Terraformcloud~10 mins

When workspaces are appropriate in Terraform - Step-by-Step Execution

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
Process Flow - When workspaces are appropriate
Start Terraform Project
Decide Environment Separation?
Use Workspaces
Deploy Resources
Manage State
End
This flow shows deciding if Terraform workspaces are needed to separate environments, then deploying resources accordingly.
Execution Sample
Terraform
terraform workspace new dev
terraform apply
terraform workspace new prod
terraform apply
Create separate workspaces for dev and prod environments and apply infrastructure changes in each.
Process Table
StepActionWorkspaceState File UsedResult
1Create workspace 'dev'devterraform.tfstate (dev)Workspace 'dev' created
2Apply infrastructuredevterraform.tfstate (dev)Resources deployed in dev
3Create workspace 'prod'prodterraform.tfstate (prod)Workspace 'prod' created
4Apply infrastructureprodterraform.tfstate (prod)Resources deployed in prod
5Switch to 'dev'devterraform.tfstate (dev)Workspace switched to dev
6Destroy infrastructuredevterraform.tfstate (dev)Resources destroyed in dev
7Switch to 'prod'prodterraform.tfstate (prod)Workspace switched to prod
8Destroy infrastructureprodterraform.tfstate (prod)Resources destroyed in prod
💡 All environments managed separately using workspaces, ensuring isolated state files.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7Final
Current Workspacedefaultdevproddevprodprod
State Fileterraform.tfstate (default)terraform.tfstate (dev)terraform.tfstate (prod)terraform.tfstate (dev)terraform.tfstate (prod)terraform.tfstate (prod)
Key Moments - 2 Insights
Why do we create separate workspaces for dev and prod instead of using one?
Separate workspaces keep state files isolated, so changes in dev do not affect prod. See steps 1-4 in execution_table.
What happens if we apply infrastructure without switching to the correct workspace?
Terraform will apply changes to the current workspace's state, possibly affecting the wrong environment. See step 5 and 7 for switching workspaces.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what workspace is active after step 3?
Adefault
Bdev
Cprod
Dnone
💡 Hint
Check the 'Workspace' column at step 3 in execution_table.
At which step does Terraform switch back to the 'dev' workspace?
AStep 5
BStep 7
CStep 2
DStep 4
💡 Hint
Look for 'Switch to dev' action in execution_table.
If you did not create separate workspaces, what would happen to the state files?
ASeparate state files automatically created
BOne shared state file for all environments
CNo state file created
DState files deleted after apply
💡 Hint
Refer to variable_tracker showing state file names per workspace.
Concept Snapshot
Terraform workspaces let you manage multiple environments in one project.
Each workspace has its own state file.
Use 'terraform workspace new <name>' to create.
Switch with 'terraform workspace select <name>'.
Apply changes per workspace to isolate environments.
Full Transcript
This visual execution shows when to use Terraform workspaces. You start by deciding if you need separate environments like dev and prod. If yes, create a workspace for each. Each workspace has its own state file, so changes in one do not affect the other. You apply infrastructure changes in each workspace separately. Switching workspaces changes which environment you manage. This keeps your infrastructure organized and safe. The execution table traces creating workspaces, applying resources, switching, and destroying them. The variable tracker shows how the current workspace and state file change over time. Key moments clarify why isolation matters and what happens if you don't switch workspaces. The quiz tests understanding of workspace states and effects on state 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

  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