Bird
Raised Fist0
Terraformcloud~10 mins

Default workspace 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 - 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.

Practice

(1/5)
1. What is the default workspace in Terraform?
easy
A. A workspace only for remote backends
B. A workspace you must create manually before use
C. A temporary workspace that deletes after each run
D. The main workspace Terraform uses automatically to store state

Solution

  1. Step 1: Understand Terraform workspaces

    Terraform uses workspaces to manage different states. The default workspace is created automatically.
  2. Step 2: Identify the role of the default workspace

    The default workspace stores the state unless you create and switch to others.
  3. Final Answer:

    The main workspace Terraform uses automatically to store state -> Option D
  4. Quick Check:

    Default workspace = main automatic workspace [OK]
Hint: Default workspace is automatic and stores state by default [OK]
Common Mistakes:
  • Thinking default workspace must be created manually
  • Assuming default workspace deletes after runs
  • Confusing default workspace with remote backend only
2. Which command shows the current active workspace in Terraform?
easy
A. terraform workspace list
B. terraform workspace show
C. terraform workspace current
D. terraform workspace status

Solution

  1. Step 1: Recall Terraform workspace commands

    Common commands include list to see all workspaces and show to display the current one.
  2. Step 2: Identify the command to show active workspace

    terraform workspace show outputs the current active workspace name.
  3. Final Answer:

    terraform workspace show -> Option B
  4. Quick Check:

    Show current workspace = terraform workspace show [OK]
Hint: Use 'terraform workspace show' to see active workspace [OK]
Common Mistakes:
  • Using 'terraform workspace list' to show current workspace
  • Assuming 'terraform workspace current' is valid
  • Confusing 'status' with workspace commands
3. Given this Terraform CLI output after running terraform workspace list:

  default
* dev
  staging
Which workspace is currently active?
medium
A. default
B. staging
C. dev
D. No workspace is active

Solution

  1. Step 1: Understand workspace list output format

    The asterisk (*) marks the currently active workspace in the list.
  2. Step 2: Identify the active workspace

    The workspace with * is dev, so it is active.
  3. Final Answer:

    dev -> Option C
  4. Quick Check:

    Asterisk marks active workspace = dev [OK]
Hint: Look for * in workspace list to find active workspace [OK]
Common Mistakes:
  • Choosing 'default' because it sounds like default
  • Ignoring the asterisk symbol
  • Assuming no workspace is active if not obvious
4. You run terraform workspace new test but get an error: "Workspace 'test' already exists." What should you do next?
medium
A. Run terraform workspace select test to switch to it
B. Delete the existing workspace with terraform workspace delete test
C. Rename the existing workspace before creating a new one
D. Run terraform init again to fix the error

Solution

  1. Step 1: Understand the error message

    The error says the workspace already exists, so you cannot create it again.
  2. Step 2: Choose the correct action

    Since it exists, you should switch to it using terraform workspace select test.
  3. Final Answer:

    Run terraform workspace select test to switch to it -> Option A
  4. Quick Check:

    Workspace exists? Select it, don't create [OK]
Hint: Select existing workspace instead of creating it again [OK]
Common Mistakes:
  • Trying to create workspace again without switching
  • Deleting workspace unnecessarily
  • Running init does not fix workspace existence
5. You want to manage two environments, prod and dev, using Terraform workspaces. Which approach correctly uses the default workspace?
hard
A. Use default workspace for dev and create/select prod workspace
B. Create both prod and dev workspaces and never use default
C. Rename default workspace to prod and create dev workspace
D. Use the default workspace for prod and create/select dev workspace

Solution

  1. Step 1: Understand default workspace role

    The default workspace is ready to use and often used for simpler or initial environments.
  2. Step 2: Apply best practice for environment separation

    Use default for dev (development) and create/select a separate workspace for prod (production) to isolate states.
  3. Final Answer:

    Use default workspace for dev and create/select prod workspace -> Option A
  4. Quick Check:

    Default workspace = dev environment [OK]
Hint: Use default for dev, create prod workspace for safety [OK]
Common Mistakes:
  • Using default for prod which risks accidental changes
  • Ignoring default workspace and creating all manually
  • Trying to rename default workspace (not supported)