Introduction Terraform workspaces allow managing multiple environments from the same configuration. When the workspace does not exist or the state backend is misconfigured, deployments fail with workspace errors.
Symptoms - `terraform workspace select prod` returns: "workspace "prod" does not exist" - Error: "Failed to select workspace" - State file not found for the workspace - Resources deployed to wrong environment - Workspace list does not show expected environments
Common Causes - Workspace never created (terraform workspace new not run) - State backend not configured for workspace support - Workspace created locally but not in remote backend - CI/CD pipeline not creating workspace before use - Backend configuration changed (S3 key prefix, workspace key delimiter)
Step-by-Step Fix 1. **List existing workspaces**: ```bash terraform workspace list # For remote backends: terraform workspace list -state="s3://my-bucket/terraform.tfstate" ```
- 1.Create missing workspace:
- 2.```bash
- 3.terraform workspace new prod
- 4.terraform workspace select prod
- 5.
` - 6.Verify remote backend workspace support:
- 7.```hcl
- 8.terraform {
- 9.backend "s3" {
- 10.bucket = "my-terraform-state"
- 11.key = "prod/terraform.tfstate"
- 12.region = "us-east-1"
- 13.workspace_key_prefix = "env"
- 14.dynamodb_table = "terraform-lock"
- 15.}
- 16.}
- 17.
` - 18.For CI/CD, create workspace before use:
- 19.```bash
- 20.terraform workspace select $TF_WORKSPACE || terraform workspace new $TF_WORKSPACE
- 21.
`