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. 1.Create missing workspace:
  2. 2.```bash
  3. 3.terraform workspace new prod
  4. 4.terraform workspace select prod
  5. 5.`
  6. 6.Verify remote backend workspace support:
  7. 7.```hcl
  8. 8.terraform {
  9. 9.backend "s3" {
  10. 10.bucket = "my-terraform-state"
  11. 11.key = "prod/terraform.tfstate"
  12. 12.region = "us-east-1"
  13. 13.workspace_key_prefix = "env"
  14. 14.dynamodb_table = "terraform-lock"
  15. 15.}
  16. 16.}
  17. 17.`
  18. 18.For CI/CD, create workspace before use:
  19. 19.```bash
  20. 20.terraform workspace select $TF_WORKSPACE || terraform workspace new $TF_WORKSPACE
  21. 21.`

Prevention - Create workspaces as part of initial setup scripts - Use environment variables (TF_WORKSPACE) for workspace selection - Document workspace naming conventions - Include workspace creation in CI/CD pipeline - Use separate state files per environment (alternative to workspaces)