0
0
Terraformcloud~10 mins

Default workspace in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Default workspace
Start Terraform
Check Workspace
Is workspace set?
NoUse Default Workspace
Load Default Workspace State
Load Selected Workspace State
Apply Terraform Commands
Update Workspace State
End
Terraform starts and checks if a workspace is selected. If none is set, it uses the default workspace to load and update infrastructure state.
Execution Sample
Terraform
terraform workspace show
terraform apply
terraform workspace list
Shows current workspace, applies changes in that workspace, then lists all workspaces.
Process Table
StepActionWorkspace SelectedState LoadedResult
1terraform workspace showdefaultdefault stateShows 'default' workspace
2terraform applydefaultdefault stateApplies changes to default workspace
3terraform workspace listdefaultdefault stateLists all workspaces including 'default'
4terraform workspace new newnewnew state (empty or existing)Creates and switches to 'new' workspace
5terraform applynewnew stateApplies changes to 'new' workspace
6terraform workspace shownewnew stateShows 'new' workspace
7terraform workspace select defaultdefaultdefault stateSwitches back to 'default' workspace
8terraform applydefaultdefault stateApplies changes to 'default' workspace
9terraform workspace showdefaultdefault stateShows 'default' workspace
10End--Execution stops
💡 Execution stops after showing the current workspace and applying changes.
Status Tracker
VariableStartAfter Step 1After Step 4After Step 7Final
workspacedefaultdefaultnewdefaultdefault
statedefault statedefault statenew statedefault statedefault state
Key Moments - 3 Insights
Why does Terraform use the 'default' workspace when no workspace is selected?
Terraform always uses the 'default' workspace if no other workspace is selected, as shown in execution_table step 1 where 'default' is active before any selection.
What happens to the state when switching workspaces?
Switching workspaces loads the state specific to that workspace, as seen in steps 4 and 7 where the state changes to 'new state' or back to 'default state'.
Can you apply changes in different workspaces independently?
Yes, each workspace has its own state, so applying changes affects only the selected workspace's state, demonstrated in steps 2 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what workspace is active after step 4?
Adefault
Bnew
Cproduction
Dstaging
💡 Hint
Check the 'Workspace Selected' column at step 4 in the execution_table.
At which step does Terraform switch back to the default workspace?
AStep 5
BStep 6
CStep 7
DStep 8
💡 Hint
Look for 'terraform workspace select default' in the Action column.
If you run 'terraform apply' in the 'new' workspace, which state is updated?
Anew state
Bdefault state
Cglobal state
Dno state
💡 Hint
Refer to the 'State Loaded' and 'Result' columns at step 5.
Concept Snapshot
Terraform Default Workspace:
- Terraform uses 'default' workspace if none selected.
- Each workspace has its own state file.
- Use 'terraform workspace select <name>' to switch.
- 'terraform apply' affects current workspace state.
- 'terraform workspace show' displays active workspace.
Full Transcript
Terraform starts by checking which workspace is active. If no workspace is selected, it uses the 'default' workspace. Each workspace has its own state file that tracks infrastructure. When you run 'terraform apply', it updates the state of the current workspace only. You can switch workspaces using 'terraform workspace select <name>'. The command 'terraform workspace show' tells you which workspace is active. This way, you can manage multiple environments or versions of your infrastructure separately.