0
0
Terraformcloud~10 mins

Creating and switching workspaces in Terraform - Visual Walkthrough

Choose your learning style9 modes available
Process Flow - Creating and switching workspaces
Start Terraform CLI
Check current workspace
Create new workspace?
NoUse current workspace
Yes
Run 'terraform workspace new <name>'
Workspace active
Apply Terraform changes in workspace
End
This flow shows how Terraform starts, checks or creates a workspace, switches to it, and applies changes there.
Execution Sample
Terraform
terraform workspace new dev
terraform workspace select dev
terraform apply
Create a new workspace named 'dev', switch to it, then apply infrastructure changes in that workspace.
Process Table
StepCommandActionWorkspace BeforeWorkspace AfterResult
1terraform workspace new devCreate new workspace 'dev'defaultdevWorkspace 'dev' created and switched to
2terraform workspace select devSelect workspace 'dev'devdevWorkspace remains 'dev'
3terraform applyApply changes in current workspacedevdevInfrastructure applied in 'dev' workspace
4terraform workspace select defaultSwitch back to 'default'devdefaultWorkspace switched to 'default'
5terraform applyApply changes in 'default'defaultdefaultInfrastructure applied in 'default' workspace
6terraform workspace select nonexistTry to select non-existent workspacedefaultdefaultError: workspace 'nonexist' does not exist
7terraform workspace listList all workspacesdefaultdefaultShows: default, dev
8Exit-defaultdefaultEnd of workspace operations
💡 Execution stops after workspace operations complete or error occurs.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 6Final
Current Workspacedefaultdevdevdefaultdefaultdefault
Key Moments - 3 Insights
Why does 'terraform workspace select dev' not change the workspace after creating it?
Because 'terraform workspace new dev' already switches to the new workspace 'dev' as shown in step 1 of the execution_table.
What happens if you try to select a workspace that does not exist?
Terraform returns an error and keeps the current workspace unchanged, as shown in step 6 of the execution_table.
How do you know which workspace is active?
The current workspace is shown before and after each command in the execution_table and can be listed with 'terraform workspace list' (step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the workspace after step 1?
Adev
Bdefault
Cnonexist
DNone
💡 Hint
Check the 'Workspace After' column for step 1 in the execution_table.
At which step does Terraform return an error due to a non-existent workspace?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for the 'Result' column mentioning an error in the execution_table.
If you skip 'terraform workspace new dev' and run 'terraform workspace select dev' first, what happens?
AWorkspace switches to 'dev' successfully
BCreates 'dev' workspace automatically
CError because 'dev' workspace does not exist
DSwitches to 'default' workspace
💡 Hint
Refer to step 6 where selecting a non-existent workspace causes an error.
Concept Snapshot
Terraform Workspaces:
- Use 'terraform workspace new <name>' to create and switch to a workspace.
- Use 'terraform workspace select <name>' to switch to an existing workspace.
- Workspaces isolate infrastructure states.
- Errors occur if selecting non-existent workspace.
- List workspaces with 'terraform workspace list'.
Full Transcript
This visual execution shows how Terraform manages workspaces. It starts with the default workspace. Creating a new workspace with 'terraform workspace new dev' creates and switches to 'dev'. Selecting the same workspace again keeps it active. Applying changes affects the current workspace only. Switching back to 'default' workspace is done with 'terraform workspace select default'. Trying to select a workspace that does not exist causes an error and keeps the current workspace unchanged. Listing workspaces shows all available workspaces. This helps isolate infrastructure states for different environments.