0
0
Terraformcloud~10 mins

Why workspaces separate environments in Terraform - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why workspaces separate environments
Start Terraform Project
Create Workspace: dev
Apply Config in dev
Create Workspace: prod
Apply Config in prod
Switch Workspace
Terraform uses workspace state
Separate environments managed
Terraform uses workspaces to keep environment states separate, allowing safe management of multiple environments like dev and prod.
Execution Sample
Terraform
terraform workspace new dev
terraform apply
terraform workspace new prod
terraform apply
terraform workspace select dev
terraform apply
This sequence creates two workspaces (dev and prod), applies infrastructure in each, and switches between them to manage separate environments.
Process Table
StepCommandWorkspace SelectedActionResult
1terraform workspace new devdevCreate new workspaceWorkspace 'dev' created and selected
2terraform applydevApply configInfrastructure deployed in 'dev' environment
3terraform workspace new prodprodCreate new workspaceWorkspace 'prod' created and selected
4terraform applyprodApply configInfrastructure deployed in 'prod' environment
5terraform workspace select devdevSwitch workspaceWorkspace switched to 'dev'
6terraform applydevApply configInfrastructure updated in 'dev' environment
💡 Execution stops after managing separate environments with distinct workspace states.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
Current Workspacedefaultdevproddevdev
Infrastructure Stateemptydev stateprod statedev statedev state updated
Key Moments - 2 Insights
Why does switching workspaces change the infrastructure state?
Each workspace has its own state file, so switching changes which environment's state Terraform uses, as shown in steps 3 and 5 in the execution_table.
Can applying in one workspace affect another workspace's infrastructure?
No, because each workspace keeps its state separate, applying in 'dev' does not affect 'prod', as seen in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which workspace is active after step 3?
Adefault
Bdev
Cprod
Dnone
💡 Hint
Check the 'Workspace Selected' column at step 3 in the execution_table.
At which step does Terraform switch back to the 'dev' workspace?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look for the 'terraform workspace select dev' command in the execution_table.
If you applied changes in 'prod' at step 6 instead of 'dev', what would change in variable_tracker?
ACurrent Workspace would be 'prod' after step 6
BInfrastructure State for 'prod' would update after step 6
CInfrastructure State for 'dev' would update after step 6
DNo changes would occur
💡 Hint
Refer to the 'Current Workspace' and 'Infrastructure State' rows in variable_tracker after step 6.
Concept Snapshot
Terraform workspaces let you manage multiple environments by keeping separate state files.
Create or switch workspaces with 'terraform workspace new/select'.
Apply infrastructure per workspace to isolate changes.
Switching workspace changes which environment Terraform manages.
This avoids mixing dev and prod resources.
Full Transcript
Terraform workspaces separate environments by keeping their state files distinct. When you create a workspace like 'dev' or 'prod', Terraform stores the infrastructure state separately. Applying changes in one workspace affects only that environment. Switching workspaces changes which environment Terraform manages. This prevents accidental changes across environments and helps organize infrastructure safely.