# Help using Terraform workspaces in an automation pipeline with TF\_WORKSPACE: Currently selected workspace "X" does not exist

**URL:** <https://discuss.hashicorp.com/t/help-using-terraform-workspaces-in-an-automation-pipeline-with-tf-workspace-currently-selected-workspace-x-does-not-exist/40676>\
**Category:** Terraform\
**Created:** [June 11, 2022, 10:05am UTC](https://discuss.hashicorp.com/t/help-using-terraform-workspaces-in-an-automation-pipeline-with-tf-workspace-currently-selected-workspace-x-does-not-exist/40676 "2022-06-11T10:05:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![briancaffey](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/briancaffey/32/26506_2.png) [@briancaffey](https://discuss.hashicorp.com/u/briancaffey)\
**Post date:** [June 11, 2022, 10:05am UTC](https://discuss.hashicorp.com/t/help-using-terraform-workspaces-in-an-automation-pipeline-with-tf-workspace-currently-selected-workspace-x-does-not-exist/40676/1 "2022-06-11T10:05:46Z")

</div>

I am working on an open source project with Terraform that will allow me to set up ad-hoc environments through GitHub Actions. Each ad-hoc environment will correspond to a terraform workspace. I’m setting the workspace by exporting `TF_WORKSPACE` before running terraform init, plan and apply. This works the first time around. For example, I’m able to create an ad-hoc environment called `alpha`. In my S3 backend I can see that the state file is saved under the `alpha` folder. The issue is that when I run the same pipeline to create another ad-hoc environment called `beta`, I get the following message:

```auto
Initializing the backend...
╷
│Error: Currently selected workspace "beta" does not exist
│
│
╵
Error: Process completed with exit code 1.

```

Here is the section of my GitHub action that is failing: [django-step-by-step/ad\_hoc\_env\_create\_update.yml at main · briancaffey/django-step-by-step · GitHub](https://github.com/briancaffey/django-step-by-step/blob/main/.github/workflows/ad_hoc_env_create_update.yml#L110-L142)

I have been over this article: [Selecting a workspace when running Terraform in automation – HashiCorp Help Center](https://support.hashicorp.com/hc/en-us/articles/360043550953-Selecting-a-workspace-when-running-Terraform-in-automation) but I’m still not sure what I’m doing wrong in my automation pipeline.

The `alpha` workspace did not exist, but it seemed to be able to create it and use it as the workspace in my first run. I’m not sure why other workspaces are not able to be created using the same pipeline.

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [June 13, 2022, 11:20pm UTC](https://discuss.hashicorp.com/t/help-using-terraform-workspaces-in-an-automation-pipeline-with-tf-workspace-currently-selected-workspace-x-does-not-exist/40676/2 "2022-06-13T23:20:50Z")

</div>

Hi @briancaffey,

Unfortunately the part of this which is surprising to me is that this worked for your `alpha` workspace, since indeed Terraform refusing to work with a non-existing workspace is by design so that Terraform can catch typos that might, for example, lead to creating an entirely new parallel set of infrastructure when one intended to update some existing infrastructure.

To make this work you’ll need to introduce an extra step into your pipeline to explicitly create the workspace. Workspace creation requires having the backend initialized first, so you’ll need to wait until after `terraform init` and `terraform workspace create` before setting the environment variable. The sequence of steps would therefore be:

- `terraform init ...`
- `terraform workspace create "${WORKSPACE}"`
- `export TF_WORKSPACE="${WORKSPACE}"`
- `terraform apply ...`
- `terraform output ...`

(Note that `terraform apply -auto-approve` already has the Terraform planning step built into it if you aren’t also providing a saved plan file, so the extra `terraform plan` step before that isn’t adding anything in your current pipeline; you’re just running the planning process twice.)

---

<div class="post-metadata">

**Author:** ![briancaffey](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/briancaffey/32/26506_2.png) [@briancaffey](https://discuss.hashicorp.com/u/briancaffey)\
**Post date:** [June 13, 2022, 11:34pm UTC](https://discuss.hashicorp.com/t/help-using-terraform-workspaces-in-an-automation-pipeline-with-tf-workspace-currently-selected-workspace-x-does-not-exist/40676/3 "2022-06-13T23:34:42Z")

</div>

Hi @apparentlymart, Thank you for your advice on this. I’m also puzzled about why the first workspace was able to be created, but a second one was not (using only `TF_WORKSPACE`). I will update my GitHub Actions script to do things in the order that you recommended and post back here with what I find.

I had tried doing something like:

```auto
terraform workspace select $WORKSPACE || terraform workspace new $WORKSPACE

```

and other combinations of these commands, but I’ll make sure I’ll try doing this in the order shown in your reply. This does make a lot of sense though. If the workspace doesn’t exist then I will need to create it.

I also saw some interesting solutions in this thread: [terraform init doesn't honor input or automation flags for invalid workspace · Issue #21393 · hashicorp/terraform · GitHub](https://github.com/hashicorp/terraform/issues/21393), but it looks like it may be specific to an older version of Terraform.

Thanks for pointing that out about `terraform apply`. I see that I don’t need it here in the way that I’m using it.

---

<div class="post-metadata">

**Author:** ![briancaffey](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/briancaffey/32/26506_2.png) [@briancaffey](https://discuss.hashicorp.com/u/briancaffey)\
**Post date:** [June 14, 2022, 11:38am UTC](https://discuss.hashicorp.com/t/help-using-terraform-workspaces-in-an-automation-pipeline-with-tf-workspace-currently-selected-workspace-x-does-not-exist/40676/4 "2022-06-14T11:38:52Z")

</div>

This worked perfectly @apparentlymart, thank you again! The key part that I wasn’t doing in some of the other ways I had tried to make this work is exporting the `TF_WORKSPACE` AFTER trying to create the workspace. I’m now doing things in the same order that you mentioned:

```auto
terraform init ...
terraform workspace create ${WORKSPACE} || echo "Workspace ${WORKSPACE} already exists or cannot be created"
export TF_WORKSPACE=$WORKSPACE
terraform apply ...
terraform output ...

```

Now it has the correct behavior of “create the workspace if it doesn’t exist, or select the existing workspace” and I can create multiple ad hoc environments in my project without pipeline failures! 🎉

---

<div class="post-metadata">

**Author:** ![timblaktu](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/timblaktu/32/14584_2.png) [@timblaktu](https://discuss.hashicorp.com/u/timblaktu)\
**Post date:** [November 23, 2022, 5:04am UTC](https://discuss.hashicorp.com/t/help-using-terraform-workspaces-in-an-automation-pipeline-with-tf-workspace-currently-selected-workspace-x-does-not-exist/40676/5 "2022-11-23T05:04:47Z")

</div>

@briancaffey I’m assuming your GitHub Actions are working in a freshly cloned workspace each build. You comments imply you are ably to assume that at the beginning of your pipeline there isn’t already a workspace selected (other than default).

FWIW, if anyone ends up here with this issue and is not able to make this assumption (bc your running on a dev workstation or a pipeline node that’s re-using the workspace without cleaning up .terraform after a workflow that switches commits/branches regularly, probably an anti-pattern, but…)…

…it seems to me you can’t always just `terraform init ...` as a first step, bc the currently selected workspace might not exist. This is an insidious situation, since it deadlocks the proposed sequence above, even when explicitly `unset`tting `TF_WORKSPACE` prior, because:

1. running `terraform init` first → `Error: Currently selected workspace "X" does not exist`
2. trying to select a known-good workspace first, e.g. `terraform select default`, → `Backend initialization required: please run "terraform init"`

I run into this from time to time and the only solution I know of is another anti-pattern which is to pull back `terraform`’s curtain a bit and hack away at its internal implementation details for “selecting a workspace” as a first step. (This is actually Option 2 in [the page you linked earlier](https://support.hashicorp.com/hc/en-us/articles/360043550953-Selecting-a-workspace-when-running-Terraform-in-automation).)

```auto
function init_workspace {
    local _TF_INIT_EXTRA_ARGS=${1:-}
    echo "Selecting workspace..."
    printf '%s' "default" > ${SCRIPT_DIR}/../.terraform/environment
    SAVED_TF_WORKSPACE=$TF_WORKSPACE
    unset TF_WORKSPACE
    terraform init -input=false ${_TF_INIT_EXTRA_ARGS}
    TF_WORKSPACE=$SAVED_TF_WORKSPACE
    terraform workspace select "${TF_WORKSPACE}" || terraform workspace new "${TF_WORKSPACE}"
}

```

---

<div class="post-metadata">

**Author:** ![andrewhharmon](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/andrewhharmon/32/35705_2.png) [@andrewhharmon](https://discuss.hashicorp.com/u/andrewhharmon)\
**Post date:** [May 8, 2024, 6:18pm UTC](https://discuss.hashicorp.com/t/help-using-terraform-workspaces-in-an-automation-pipeline-with-tf-workspace-currently-selected-workspace-x-does-not-exist/40676/6 "2024-05-08T18:18:26Z")

</div>

I believe this can now be solved with the `-or-create` flag of the `workspace select` command.  
`terraform workspace select -or-create "${TF_WORKSPACE}"`

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [May 15, 2024, 6:19pm UTC](https://discuss.hashicorp.com/t/help-using-terraform-workspaces-in-an-automation-pipeline-with-tf-workspace-currently-selected-workspace-x-does-not-exist/40676/7 "2024-05-15T18:19:08Z")

</div>

Closed on May 15, 2024, 6:19pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
