0
0
Terraformcloud~10 mins

Workspaces and remote state in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Workspaces and remote state
Start Terraform
Select Workspace
Load Remote State
Apply Changes
Update Remote State
End
Terraform starts by selecting a workspace, loads the remote state for that workspace, applies changes, then updates the remote state.
Execution Sample
Terraform
terraform workspace new dev
terraform workspace select dev
terraform apply
terraform state pull
Create and select a workspace named 'dev', apply infrastructure changes, then pull the remote state.
Process Table
StepActionWorkspaceState LoadedResult
1Create workspace 'dev'devNo (new workspace)Workspace 'dev' created
2Select workspace 'dev'devNo (just selected)Workspace 'dev' selected
3Load remote statedevYes (empty or existing)State loaded for 'dev'
4Apply changesdevYesInfrastructure updated, state changed
5Update remote statedevYesRemote state updated with new info
6EnddevYesProcess complete
💡 Process stops after updating remote state and completing apply.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Workspacedefaultdevdevdevdevdevdev
State LoadedNoNoNoYesYesYesYes
InfrastructureInitialInitialInitialInitialUpdatedUpdatedUpdated
Key Moments - 3 Insights
Why does Terraform load a different state after selecting a workspace?
Each workspace has its own separate state file. Selecting a workspace changes which remote state Terraform loads, as shown in step 3 of the execution table.
What happens if you apply changes without selecting a workspace?
Terraform uses the default workspace and its state. This means changes apply to that environment, not to others like 'dev' or 'prod'. See step 2 where workspace selection matters.
How does remote state help when using multiple workspaces?
Remote state stores the current infrastructure info for each workspace separately. This keeps environments isolated and consistent, as shown by the state loaded and updated in steps 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the workspace after step 2?
Adev
Bdefault
Cprod
DNone
💡 Hint
Check the 'Workspace' column in row for step 2.
At which step does Terraform load the remote state for the workspace?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for 'Load remote state' action in the execution table.
If you skip creating a new workspace and apply directly, what workspace is used?
Adev
Bdefault
Cprod
DNo workspace
💡 Hint
Refer to variable_tracker 'Workspace' start value and key moments about default workspace.
Concept Snapshot
Terraform Workspaces:
- Separate environments with isolated state files
- Use 'terraform workspace new NAME' to create
- Use 'terraform workspace select NAME' to switch
Remote State:
- Stores current infra info remotely
- Keeps state consistent across users
- Each workspace has its own remote state
Full Transcript
Terraform uses workspaces to separate environments like development and production. Each workspace has its own remote state file stored remotely, which Terraform loads when you select that workspace. When you apply changes, Terraform updates the infrastructure and then updates the remote state for that workspace. This keeps environments isolated and consistent. The default workspace is used if no other is selected. Creating and selecting workspaces changes which remote state is loaded and updated.