Workspaces
Terraform workspaces allow you to manage multiple distinct sets of infrastructure resources using the same configuration. Each workspace has its own state file, enabling you to maintain separate environments like development, staging, and production.What They Do
Workspaces provide:- Isolated state files for each environment
- Same configuration used across multiple deployments
- Easy switching between environments
- Separate resource instances per workspace
- Built-in environment management without additional tooling
When to Use Them
Use workspaces when you want to:- Deploy the same infrastructure configuration to multiple environments
- Test changes in a development workspace before promoting to production
- Maintain separate state for each team member’s development environment
- Create temporary environments for testing or feature development
- Manage multiple regions or accounts with similar infrastructure
Workspace Basics
Default Workspace
Every Terraform configuration starts with a workspace nameddefault:
Current Workspace
Check which workspace you’re currently using:Working with Workspaces
List Workspaces
Create a New Workspace
Create Workspace from Existing State
Create a workspace and populate it with an existing state file:Switch Between Workspaces
Create Workspace if Not Exists
Select a workspace and create it if it doesn’t exist:Delete a Workspace
Warning: If the workspace contains resources, you’ll see:
Using Workspaces in Configuration
Referencing Current Workspace
Useterraform.workspace to reference the current workspace name:
- development: Creates
t2.microinstance taggedweb-development - production: Creates
t3.largeinstance taggedweb-production
Workspace-Specific Variables
Use different variable files per workspace:development.tfvarsstaging.tfvarsproduction.tfvars
Conditional Resources
Create resources only in specific workspaces:Backend Configuration Per Workspace
Some backends automatically isolate workspace state: S3 Backend:defaultworkspace:s3://my-terraform-state/project/terraform.tfstatestagingworkspace:s3://my-terraform-state/env:/staging/project/terraform.tfstateproductionworkspace:s3://my-terraform-state/env:/production/project/terraform.tfstate
defaultworkspace:terraform.tfstatedevelopmentworkspace:terraform.tfstate.d/development/terraform.tfstateproductionworkspace:terraform.tfstate.d/production/terraform.tfstate
Workspace Commands Reference
terraform workspace list
List all workspaces:terraform workspace show
Show the current workspace name:terraform workspace new
Create a new workspace:-lock=false- Don’t hold a state lock during the operation-lock-timeout=DURATION- Duration to retry a state lock-state=PATH- Copy an existing state file into the new workspace
terraform workspace select
Switch to a different workspace:-or-create- Create the workspace if it doesn’t exist
terraform workspace delete
Delete a workspace:-lock=false- Don’t hold a state lock during the operation-lock-timeout=DURATION- Duration to retry a state lock-force- Remove workspace even if it contains resources
Best Practices
Workspace Naming Conventions
Use clear, descriptive names:Verify Before Destructive Operations
Always confirm workspace before apply or destroy:Use Workspace-Aware Naming
Include workspace name in resource names:- development:
myapp-data-development - production:
myapp-data-production
Avoid Workspace Interpolation in Backend
Don’t useterraform.workspace in backend configuration:
Limit Workspace Use Cases
Workspaces are best for:- Similar environments (dev, staging, prod of the same app)
- Temporary environments (feature branches, testing)
- Personal development environments
- Different applications
- Significantly different architectures
- Different regions or accounts (though workspaces can work here too)
Document Workspace Purpose
Maintain a README documenting workspaces:Protect Production Workspace
Add safeguards for production:Clean Up Temporary Workspaces
Regularly remove unused workspaces:Workspace Environment Variable Override
TheTF_WORKSPACE environment variable overrides workspace selection:
Troubleshooting
Cannot Delete Active Workspace
Workspace Not Found
Invalid Workspace Name
Lost Workspace State
If you’ve lost a workspace’s state file:Workspace Locked
Alternatives to Workspaces
Separate Directories
Use different directories for each environment:Separate Repositories
Use different Git repositories for significantly different environments.Terragrunt
Use Terragrunt for more sophisticated environment management.Next Steps
After setting up workspaces:- Document your workspace strategy for your team
- Set up workspace-specific variable files
- Configure CI/CD to handle multiple workspaces
- Implement workspace protection for production
- Regularly audit and clean up unused workspaces