Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform workspace select - Option to create if it doesnt exist #16191

Closed
ryudice opened this issue Sep 26, 2017 · 16 comments · Fixed by #31633
Closed

Terraform workspace select - Option to create if it doesnt exist #16191

ryudice opened this issue Sep 26, 2017 · 16 comments · Fixed by #31633

Comments

@ryudice
Copy link

ryudice commented Sep 26, 2017

Can we have a switch for the workspace select command that creates the workspace if it doesnt exist?

@apparentlymart
Copy link
Member

Interesting idea, @ryudice!

I assume your motivation here is running Terraform in some sort of automation that imposes a fixed naming convention for workspaces across several configurations, and so you want it to be able to work the same for an existing configuration (where the workspaces were already created) and for a new one (where they haven't been, yet.)

Maybe this would look something like this:

terraform workspace select -or-create foo

The -or-create option would activate this behavior, causing this function to behave like terraform workspace new if the workspace doesn't already exist.

@ciaranmcveigh5
Copy link

ciaranmcveigh5 commented Nov 8, 2017

Bash fix/hack we use in our CI

terraform workspace select $env_name || terraform workspace new $env_name

Will execute second command if first fails

@roman-cnd
Copy link

workflow with workspaces should be similar to one for the states, where init command is idempotent: terraform workspace init blah

@eliasdorneles
Copy link

In my case, I'm using TF_WORKSPACE in CI, so I'm doing:

terraform workspace new $TF_WORKSPACE || true

@RobRoseKnows
Copy link

Hey these work-arounds are useful, but this seems like a fairly mundane feature to add. Would this be a reasonable first contribution?

@baurmatt
Copy link
Contributor

Would be really great to get this implemented! I currently have to do this in Gitlab CI because every command which fails aborts the job:

    - terraform init -backend-config="conn_str=postgres://${TF_PG_USER}:${TF_PG_PASSWORD}@${TF_PG_HOST}/${TF_PG_DB}" -input=false || true # Fails on first run because workspace isn't yet there.
    - terraform workspace select $TF_WORKSPACE || terraform workspace new $TF_WORKSPACE # Create workspace on first run
    - terraform init -backend-config="conn_str=postgres://${TF_PG_USER}:${TF_PG_PASSWORD}@${TF_PG_HOST}/${TF_PG_DB}" -input=false # Ensure there wasn't any other 'init' error we missing above because we ignored the exit code

@baurmatt
Copy link
Contributor

This workaround broke with Terraform 0.12.8:

0.12.7:

$ terraform-0.12.7 workspace select default &> /dev/null; echo $?
1

0.12.8:

$ terraform-0.12.8 workspace select default &> /dev/null; echo $?
0

See #24427 for more information.
This means the || won't trigger the creation of a new workspace.

@ragsonline
Copy link

What is the alternative at the moment to create workspace if doesn't exists? if we need to do that in pipelines?

@eliasdorneles
Copy link

What I've been using is just always running the command to create, and ignoring the error if it already exists:

terraform workspace new MY_WORKSPACE || true

@ricoli
Copy link

ricoli commented Apr 20, 2020

or terraform workspace select MY_WORKSPACE || terraform workspace new MY_WORKSPACE if you want it to be a bit more expressive - literally "If I can't select the workspace I'll try to create it". terraform workspace new will also select it after creating btw

@bbnathan
Copy link

The problem shows up in our pipelines when we have multiple parallel scripted Jenkinsfile stages. Each parallel stage uses a different TF_DATA_DIR, different tfvars files as inputs and different sets of credentials for both the cloud-provider and the remote state backend. Having to add two more stages (workspace_create, workspace_select) to the existing init, validate, plan and apply stages is cumbersome. It would be nice if a 'workspace select' would optionally create a new workspace if it did not exist and/or a 'workspace create' (as is the pattern with 'init') would not fail if the workspace already existed. This is particularly problematic with the PostgreSQL remote state backend--a conversation for a separate thread.

@neilstuartcraig
Copy link

I would love a "create if doesn't exist" on workspace select or just an option so that workspace new exits with a return code of 0 if the workspace already exists.

The problem I have is that I am using the Docker image for Terraform in Google Cloud Build and there's no way (that I can find) to append a || true to the workspace new command (I tried and Terraform rejects the command).

There doesn't really seem to be a way around this currently for my use case so I have to have an ugly "do this after you've deployed once" step.

@brittandeyoung
Copy link
Contributor

brittandeyoung commented Aug 12, 2022

I just opened a pull request with what I think will provide what is being asked for in this issue. I would love some feedback on if this PR would fully satisfy this issue request or not.

This Pull request is taking the approach of implementing a new terraform workspace subcommand selectornew. This new sub command will have the behavior of selecting the workspace if it exists, and creating + selecting the workspace if it does not exist.

To me it was better to have a new self describing subcommand, then to add a flag to an existing subcommand, but again would love feedback on this idea.

@brittandeyoung
Copy link
Contributor

Pr has been update to follow the suggestion of @apparentlymart and use the or-create flag on the select subcommand.

@pri-me
Copy link

pri-me commented Nov 11, 2022

While we are waiting, here is another workaround :-)

if $(terraform workspace list | grep -q "${ENV}") ; then terraform workspace select ${ENV}; else terraform workspace new ${ENV}; fi

$ENV is the new workspace you want to select or create. If it already exists (contained in workspace list), then select it, otherwise run the new command, which creates it and switches to it.

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.