Skip to content

CICD-241 · Terraform workspace management ​

Encyclopedia · Terraform and infrastructure as code · All snippets

Previous · CICD-240 · Next · CICD-242

1. Where it fits ​

  • Placement CI run step or authorized lab terminal.
  • Prerequisites trusted Linux runner, Terraform CLI, reviewed IaC and a disposable state for writes.
  • Platforms local-linux, github-actions, gitea-actions.
  • Parameters TF_WORKSPACE.

2. Copy the snippet ​

bash
terraform workspace select "${TF_WORKSPACE:?}"
test "$(terraform workspace show)" = "$TF_WORKSPACE"

3. How it works ​

The selected workspace changes which state Terraform reads. Treat the backend and identity as separate boundaries. The shell predicate makes the expected state explicit and returns a nonzero status when it is not satisfied.

4. Verify the result ​

Select a pre-existing workspace and fail closed on mismatch. Separate production credentials and state rather than using workspace names as access control.

Validation syntax-validated (Bash parse). Status definitions.