0
0
Terraformcloud~10 mins

Workspaces vs directory-based separation in Terraform - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Process Flow - Workspaces vs directory-based separation
Start: Choose separation method
Use Workspaces
Single directory
Switch workspace
Isolate state files
Use directory-based
Multiple directories
Separate configs & states
Manage independently
You start by choosing how to separate your Terraform environments: either by switching workspaces in one directory or by using separate directories for each environment.
Execution Sample
Terraform
terraform workspace new dev
terraform apply
terraform workspace new prod
terraform apply
This code creates a new workspace 'dev', applies infrastructure, then switches to 'prod' workspace and applies infrastructure separately.
Process Table
StepActionWorkspace/DirectoryState File UsedEffect
1Initialize Terraform directorydefaultdefault.tfstateReady to manage infrastructure
2Create new workspace 'dev'devdev.tfstateNew isolated state created for dev
3Apply infrastructure in 'dev'devdev.tfstateResources created tracked in dev state
4Switch to workspace 'prod'prodprod.tfstateSwitch state to prod, no resources yet
5Apply infrastructure in 'prod'prodprod.tfstateResources created tracked in prod state
6Use directory 'dev_env' insteaddev_env directorydev_env/terraform.tfstateSeparate config and state for dev_env
7Apply infrastructure in 'dev_env'dev_env directorydev_env/terraform.tfstateResources created tracked in dev_env state
8Use directory 'prod_env' insteadprod_env directoryprod_env/terraform.tfstateSeparate config and state for prod_env
9Apply infrastructure in 'prod_env'prod_env directoryprod_env/terraform.tfstateResources created tracked in prod_env state
10End of demonstration--Separation by workspace or directory completed
💡 Finished showing how workspaces and directory-based separation isolate Terraform states and configurations.
Status Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
Current Workspace/Directorydefaultdevproddev_env directoryprod_env directoryprod_env directory
State File Useddefault.tfstatedev.tfstateprod.tfstatedev_env/terraform.tfstateprod_env/terraform.tfstateprod_env/terraform.tfstate
Key Moments - 3 Insights
Why does switching workspaces not require changing directories?
Because workspaces keep separate state files within the same directory, so you can switch environments without moving folders, as shown in steps 2 to 5 in the execution_table.
How does directory-based separation isolate environments differently from workspaces?
Directory-based separation uses completely separate folders with their own configs and state files, so each environment is fully independent, as seen in steps 6 to 9.
Can workspaces share configuration files?
Yes, all workspaces share the same configuration files in one directory but keep their states separate, unlike directory-based separation where configs differ per folder.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what state file is used after creating the 'dev' workspace?
Adefault.tfstate
Bdev.tfstate
Cprod.tfstate
Ddev_env/terraform.tfstate
💡 Hint
Check Step 2 in the execution_table where the 'dev' workspace is created.
At which step does the separation method switch from workspaces to directory-based?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Look for when the workspace name changes to a directory name in the execution_table.
If you apply infrastructure in the 'prod_env' directory, which state file is updated?
Aprod.tfstate
Bdefault.tfstate
Cprod_env/terraform.tfstate
Ddev.tfstate
💡 Hint
Refer to Step 9 in the execution_table for the directory-based separation state file.
Concept Snapshot
Terraform environments can be separated by:
- Workspaces: single directory, multiple state files per workspace
- Directory-based: separate folders, each with own config and state
Workspaces share configs but isolate states.
Directories isolate both configs and states.
Choose based on project complexity and team needs.
Full Transcript
This visual execution compares two ways to separate Terraform environments: workspaces and directory-based separation. Workspaces let you switch between isolated states inside one directory, sharing the same configuration files. Directory-based separation uses different folders for each environment, each with its own configs and state files. The execution table shows creating and switching workspaces, then applying infrastructure, followed by using separate directories for environments. Variable tracking shows how the current workspace or directory and state files change step-by-step. Key moments clarify why workspaces don't need directory changes and how directories isolate configs. The quiz tests understanding of state files and steps where separation methods change.