Terraform workspaces allow multiple state files to coexist in the same backend configuration. Each workspace maps to an environment (dev/staging/prod), enabling the same HCL to deploy isolated instances without directory duplication.
Promotion pipeline: deploy through dev → staging → prod with workspace selection at each stage.
Workspaces share the same backend config (storage account, container, key). They cannot have different backend locations. For strong prod/dev isolation, use separate Terraform configurations + backends.
Reference the current workspace as terraform.workspace in HCL. Use it for conditional logic: instance_type = terraform.workspace == 'prod' ? 'Standard_D4s_v5' : 'Standard_B2s'.
Workspaces suit identical environments with different sizes (dev/staging/prod). The directory pattern (separate roots per env) suits environments with different resource structures. Know the trade-offs.
In Azure Blob Storage backend, workspace state files are stored as env:/<workspace>/terraform.tfstate. The default workspace uses the key value from the backend config directly.
Create workspaces once (dev/staging/prod) as part of bootstrap. In CI pipelines, use workspace select only — never workspace new. Prevents duplicate environment accidents.
Keep a vars/ directory: vars/dev.tfvars, vars/prod.tfvars. In CI, pass -var-file=vars/${WORKSPACE}.tfvars. Never hardcode environment values in main.tf.
Sign in to share your feedback and join the discussion.