0
0
Terraformcloud~5 mins

When workspaces are appropriate in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to use the same infrastructure setup but keep different versions separate, like for testing and production. Terraform workspaces help you do this by letting you switch between different copies of your infrastructure state easily.
When you want to manage separate environments like development, staging, and production using the same Terraform code.
When you need to test changes in a safe environment without affecting the live setup.
When you want to keep infrastructure states isolated but avoid duplicating code.
When multiple teams share the same Terraform configuration but manage their own resources separately.
When you want to quickly switch between different infrastructure setups without reconfiguring everything.
Commands
This command shows all the workspaces available in your current Terraform project. It helps you see which environments you can switch to.
Terminal
terraform workspace list
Expected OutputExpected
* default dev prod
This creates a new workspace called 'dev'. You use this to start managing a separate environment without affecting others.
Terminal
terraform workspace new dev
Expected OutputExpected
Created and switched to workspace "dev". You're now on a new workspace called "dev".
This switches your current workspace to 'dev'. All Terraform commands now apply to this workspace's state.
Terminal
terraform workspace select dev
Expected OutputExpected
Switched to workspace "dev".
This applies your Terraform configuration to the current workspace without asking for confirmation. It creates or updates resources for the selected environment.
Terminal
terraform apply -auto-approve
Expected OutputExpected
Terraform used the selected workspace "dev". Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip interactive approval before applying changes
This shows the name of the workspace you are currently working in. It helps confirm you are in the right environment.
Terminal
terraform workspace show
Expected OutputExpected
dev
Key Concept

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

Common Mistakes
Not switching to the correct workspace before running terraform apply
Terraform will apply changes to the wrong environment, causing unexpected updates or downtime.
Always run 'terraform workspace select <workspace-name>' before applying changes.
Trying to share resources between workspaces directly
Workspaces keep states isolated, so resources in one workspace are invisible to others, causing confusion.
Manage shared resources outside of workspace-specific configurations or use separate Terraform projects.
Assuming workspaces create completely separate infrastructure setups
Workspaces only separate state files; the configuration code is shared, so changes affect all workspaces unless conditionally handled.
Use variables or conditional logic in your Terraform code to customize resources per workspace.
Summary
Use 'terraform workspace new' to create separate environments like dev or prod.
Switch between environments with 'terraform workspace select' before applying changes.
Check your current workspace with 'terraform workspace show' to avoid mistakes.