0
0
Terraformcloud~10 mins

When workspaces are appropriate in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - When workspaces are appropriate
Start Terraform Project
Decide Environment Separation?
Use Workspaces
Deploy Resources
Manage State
End
This flow shows deciding if Terraform workspaces are needed to separate environments, then deploying resources accordingly.
Execution Sample
Terraform
terraform workspace new dev
terraform apply
terraform workspace new prod
terraform apply
Create separate workspaces for dev and prod environments and apply infrastructure changes in each.
Process Table
StepActionWorkspaceState File UsedResult
1Create workspace 'dev'devterraform.tfstate (dev)Workspace 'dev' created
2Apply infrastructuredevterraform.tfstate (dev)Resources deployed in dev
3Create workspace 'prod'prodterraform.tfstate (prod)Workspace 'prod' created
4Apply infrastructureprodterraform.tfstate (prod)Resources deployed in prod
5Switch to 'dev'devterraform.tfstate (dev)Workspace switched to dev
6Destroy infrastructuredevterraform.tfstate (dev)Resources destroyed in dev
7Switch to 'prod'prodterraform.tfstate (prod)Workspace switched to prod
8Destroy infrastructureprodterraform.tfstate (prod)Resources destroyed in prod
💡 All environments managed separately using workspaces, ensuring isolated state files.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7Final
Current Workspacedefaultdevproddevprodprod
State Fileterraform.tfstate (default)terraform.tfstate (dev)terraform.tfstate (prod)terraform.tfstate (dev)terraform.tfstate (prod)terraform.tfstate (prod)
Key Moments - 2 Insights
Why do we create separate workspaces for dev and prod instead of using one?
Separate workspaces keep state files isolated, so changes in dev do not affect prod. See steps 1-4 in execution_table.
What happens if we apply infrastructure without switching to the correct workspace?
Terraform will apply changes to the current workspace's state, possibly affecting the wrong environment. See step 5 and 7 for switching workspaces.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what workspace is active after step 3?
Adefault
Bdev
Cprod
Dnone
💡 Hint
Check the 'Workspace' column at step 3 in execution_table.
At which step does Terraform switch back to the 'dev' workspace?
AStep 5
BStep 7
CStep 2
DStep 4
💡 Hint
Look for 'Switch to dev' action in execution_table.
If you did not create separate workspaces, what would happen to the state files?
ASeparate state files automatically created
BOne shared state file for all environments
CNo state file created
DState files deleted after apply
💡 Hint
Refer to variable_tracker showing state file names per workspace.
Concept Snapshot
Terraform workspaces let you manage multiple environments in one project.
Each workspace has its own state file.
Use 'terraform workspace new <name>' to create.
Switch with 'terraform workspace select <name>'.
Apply changes per workspace to isolate environments.
Full Transcript
This visual execution shows when to use Terraform workspaces. You start by deciding if you need separate environments like dev and prod. If yes, create a workspace for each. Each workspace has its own state file, so changes in one do not affect the other. You apply infrastructure changes in each workspace separately. Switching workspaces changes which environment you manage. This keeps your infrastructure organized and safe. The execution table traces creating workspaces, applying resources, switching, and destroying them. The variable tracker shows how the current workspace and state file change over time. Key moments clarify why isolation matters and what happens if you don't switch workspaces. The quiz tests understanding of workspace states and effects on state files.