terraform
terraform copied to clipboard
New Terraform Workspace select flag: `-or-create`
This adds the new terraform workspace select flag -or-create
. This flag is intended to make managing workspaces easier in CI environments where auto creating or selecting a workspace is useful (especially when using remote backend). This flag will first attempt to select the provided workspace name. If this workspace is not found, it will then create and select it.
Traditionally this functionality has been achieved through some sort of scripting. We currently get this behavior using the following script: terraform workspace select test || terraform workspace new test
; however, this does not work well when attempting to use the official terraform docker image as it does not handle shell scripts by design. Having this functionality in the terraform cli itself would be very useful. We want to be able to provide the same functionally locally to our developers as exists in our CI environment. Using the terraform official docker image helps provide this, but missing this functionality is making it difficult to auto provision workspaces for new projects. Calling the workspace new
command will error if it already exists, and calling the workspace select
command will error if it does not exist.
This will especially help when using terraform
with https://dagger.io or any other docker based CI as it relies on docker for running pipelines where bash scripting my not be an option.
Closes: #16191
Although I cannot commit to having this PR reviewed at this time, we acknowledge your contribution and appreciate it!
@crw Does this comment mean that this PR will not be reviewed, or that there will be a delay in the review? This feature will be a huge help to those running terraform using docker.
Hi @brittandeyoung, thanks for the bump. First off thanks for the great initial write-up for the PR - that is always a huge help.
The initial feedback is that per https://github.com/hashicorp/terraform/issues/16191#issuecomment-332340691, a flag would be more appropriate than a new subcommand. Implementing this feature as a flag would make it more likely to get reviewed. Thanks for this submission, and apologies for the late response on this.
@crw Thank you for the direction. I will modify this PR to match what is requested in #16191 and update.
@crw Pull request has been update to follow the strategy suggested. It is ready for review when you have time.
Here is a screen shot running the binary local with both the select a valid state, select a missing state, and select a missing state with new -or-create
flag.

Friendly bump @crw. Do we have an idea as to when or if this pull request will be able to be reviewed? The pull request is much smaller now using the flag.
Hi @brittandeyoung, thanks for the bump! I admit I lost track of this due to the long Labor Day weekend. We are working on finishing up the 1.3 release so apologize if this process is going a bit slowly. There is a bit of functionality coming that may overlap with this feature -- I'm looking into whether it makes sense to review this or wait for the new functionality. I'll keep you updated. Thanks for your patience.
@crw Thank you for the update. I will wait to hear back on what you find in terms of functionality overlap.
@crw Looks like 1.3.0 release is out. I did not see any potential overlap in the release notes. Any update on that end?
@brittandeyoung Yes, apologies - @jbardin moving forward with the review is the acknowledgement that we are reviewing this for inclusion. I'll check with @jbardin on what we need to do to press "merge." Thanks again for the contribution!
@crw and @jbardin Any update on this PR?
@crw ans @jbardin any update on this pr? Am I missing a required change?
Hi @brittandeyoung, this is still on @jbardin's radar. He's just been busy closing issues in the 1.3 release of Terraform. This and one other community PR are in the pipe to be reviewed once 1.3 is stable. Thanks for checking in, apologies for the stop-and-start here.
@crw thank you for the response. I will patiently wait for y'all to get caught up. I just wanted to make sure I wasn't missing something.
Quick update that I have not forgotten this, we are still working on bugs from 1.3. This is still in the queue to be reviewed.
@crw thank you for the update 👍
@crw and @jbardin . Friendly bump to make sure this didn't fall of the radar.
This would be a great feature for us, as our action feels needlessly complicated just to deal with the potential creation of a workspace :)
1.3 seems to have settled out - I'll get this back in the queue. Thanks!
@brittandeyoung is attempting to deploy a commit to the HashiCorp Team on Vercel.
A member of the Team first needs to authorize it.
Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.
@jbardin @crw Any plans for which release this flag will be available?
1.4, which is the next non-bugfix release. I believe it should start showing up in the alpha builds when we do the next one of those, which will likely be in January.
Cheers :)
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.