-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Comments
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:
The |
Bash fix/hack we use in our CI
Will execute second command if first fails |
workflow with workspaces should be similar to one for the states, where |
In my case, I'm using
|
Hey these work-arounds are useful, but this seems like a fairly mundane feature to add. Would this be a reasonable first contribution? |
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 |
This workaround broke with Terraform 0.12.8: 0.12.7:
0.12.8:
See #24427 for more information. |
What is the alternative at the moment to create workspace if doesn't exists? if we need to do that in pipelines? |
What I've been using is just always running the command to create, and ignoring the error if it already exists:
|
or |
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. |
I would love a "create if doesn't exist" on 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 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. |
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 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. |
Pr has been update to follow the suggestion of @apparentlymart and use the |
While we are waiting, here is another workaround :-)
$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 |
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. |
Can we have a switch for the workspace select command that creates the workspace if it doesnt exist?
The text was updated successfully, but these errors were encountered: