0
0
Terraformcloud~5 mins

Why workspaces separate environments in Terraform - Why It Works

Choose your learning style9 modes available
Introduction
When managing infrastructure, you often need separate setups for testing, development, and production. Terraform workspaces help keep these environments separate using the same configuration, avoiding conflicts and mistakes.
When you want to test changes without affecting your live production setup
When you need to maintain multiple versions of infrastructure for different teams
When you want to deploy the same infrastructure in different regions or accounts
When you want to keep your development and production environments isolated
When you want to reuse the same Terraform code but with different settings
Commands
This command creates a new workspace named 'dev' to isolate development environment resources.
Terminal
terraform workspace new dev
Expected OutputExpected
Created and switched to workspace "dev". You are now using workspace "dev".
Lists all existing workspaces and shows which one is currently active.
Terminal
terraform workspace list
Expected OutputExpected
* dev default
Applies the Terraform configuration in the current workspace, creating or updating resources for that environment.
Terminal
terraform apply
Expected OutputExpected
Terraform used the selected workspace "dev". Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Switches back to the 'default' workspace, usually used for production or main environment.
Terminal
terraform workspace select default
Expected OutputExpected
Switched to workspace "default".
Applies the Terraform configuration in the 'default' workspace, managing production environment resources separately from 'dev'.
Terminal
terraform apply
Expected OutputExpected
Terraform used the selected workspace "default". Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Key Concept

If you remember nothing else, remember: Terraform workspaces let you use the same code to manage separate environments safely.

Common Mistakes
Not switching to the correct workspace before running terraform apply
This causes changes to be applied to the wrong environment, risking production stability or mixing resources.
Always run 'terraform workspace select <name>' to switch to the right environment before applying changes.
Assuming workspaces create completely isolated infrastructure backends
Workspaces share the same backend but separate state files; misunderstanding this can cause confusion about resource isolation.
Understand that workspaces isolate state but not backend configuration; use separate backends if full isolation is needed.
Summary
Create and switch workspaces to separate environments using 'terraform workspace new' and 'terraform workspace select'.
Apply infrastructure changes in the active workspace to keep environments isolated.
List workspaces to see all environments and the current active one.