-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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 init doesn't honor input or automation flags for invalid workspace #21393
Comments
Looks like the recently merged #21234 doesn't take this into account either. I think if either of these "no prompt" flags were specified, it might be reasonable to select workspace 1 (index 0) automatically so that subsequent commands to list/select workspaces work properly. |
Thanks for reporting this, @mattlqx! An interactive prompt is clearly not appropriate for non-interactive automated scenarios, so we should indeed find a different way to address this for those. Automatically selecting an implied workspace feels a little too magical/risky to me, since the worst case there is applying changes intended for one workspace to another workspace. I feel like the baseline here is that the command should fail outright if it doesn't have enough information to proceed and can't obtain it via user input, which then brings us to another question: how can we give it enough information to proceed, in non-interactive mode? One way to address this in automation scenarios would be to make the workspace selection respect the
If How does that sound, @mattlqx? |
I hear you on the point of not defaulting. If we're going to outright fail the init, then there still needs to be some lightweight way for determining what workspaces ARE available by automation. There's a bit of chicken and egg here on init and workspace so I'm not sure what's feasible there. Is it possible to query my Terraform Cloud account to determine what project's workspaces are available, even if it's out-of-band, so automation can determine what needs done? e.g. Creating a new workspace before trying to select it in the project. Would adding a |
... seems like an excellent solution to allow advanced usages without behind-the-scenes magic that might hurt new users. We use Terraform (but not Terraform Cloud). Transient, on-demand workspaces created by the build server are quite useful. With versions < 0.11.14, |
Hi @mattlqx! Thanks for the additional context. In the automation scenarios I've seen before, the automation itself is choosing a specific workspace to work with, and so it doesn't need to know anything other than the single specific workspace name it's been configured with. It sounds like you have something different in mind. If you'd be willing, I'd love to hear more about the use-case. If you had a way to get a machine-readable list of workspaces to choose from, what exactly would your automation do with it? Your original suggestion seemed to lean towards just picking the first item from the list, but then I wonder how the automation would ever work with a workspace other than the one that happens to sort first? Terraform itself is hitting an API on Terraform Cloud to enumerate the workspaces matching the given prefix, so in principle you could mimic that logic, though I'd like to understand better what exactly you're trying to achieve. |
Sure. The reason why I'm asking for such a thing mostly is for migration of states from what we're currently using today (stored via the Long story short, I have a bash script that handles the backend block generation among other things (grabbing other config data from various places and setting it via TF_VAR env vars, installing third-party plugins, etc), runs The real trick is determining if the current Consul state which represents a single environment (workspace, effectively I've gotten my desired behavior today with another
This probably sounds hilariously overcomplicated but it started out with the best of intentions and as always snowballs into something else. :) Once all my projects and environments are migrated over (precisely 170 of them), I agree I wouldn't have much need to list workspaces prior to init. My Anyway, thanks for the interest, I really appreciate the effort to understand. |
Thanks for all of that great additional context, @mattlqx. Migrating from a world with only
In principle, setting One option available to you today -- though I realize not as convenient as just having Terraform figure this out itself -- is to hit the Organization Workspaces operation on the Terraform Cloud API, which can tell you which workspaces exist for your entire organization in one go, which you could then filter down to just the ones relevant for your current configuration. When I did something similar in a previous job (before I joined HashiCorp) I ended up writing a script that didn't really use Terraform CLI at all, and instead just interacted with the different APIs directly. I suggest this only to note that it is possible, understanding that it's not particularly convenient, but an API-only path here could be:
I preferred an approach like this because it allowed me to get the whole migration done in one shot, rather than gradually over time. Of course, it being one-shot could be considered a disadvantage in another context, so again I'm suggesting this only to describe what is possible right now, in case one of these answers feels like enough to get you unblocked for your immediate work, rather than waiting for changes to Terraform CLI itself. With all of that said, I'll pass this issue on to the Terraform Cloud team (who are primarily responsible for this part of Terraform CLI) so they can think about what might be best here. |
Setting TF_WORKSPACE worked for me in Azure Devops and using the remote backend against TFE free. |
This is what I've had to do to get around this issue...there might be something easier, so please let me know. Before running anything in an automated environment (in my case, Bitbucket Pipelines), you MUST create at least 1 workspace manually. This is unfortunate, but it only has to be done once. Then, the following works for all new workspaces:
Essentially, by making the |
@adback03 Thanks for your workaround. It almost worked for me with a few tweaks. I am using the S3 backend. With that backend, there always appears to be a "default" workspace even if no state for one exists in S3. With that, I was able to get the following to work without a manually created workspace:
We set the workspace by the environment |
This bug seems to be fixed in the terraform |
The interactive prompt still happens for me with
|
I took some inspiration from @apparentlymart's mentions of using the Terraform Enterprise API, and came up with what I think is a decent solution to this for me, that doesn't require any manual pre-creation of workspaces. This feels really over-engineered. It would be great if some kind of logic could be introduced to handle running in automation + Terraform Cloud workspaces. The suggestion of a
Other I'm using the YAML schema to define my pipeline in Azure DevOps. This is
The azure-devops-templates repository under the my-project project in Azure DevOps repos contains the following files.
|
Many thanks guys for interesting ideas. So here is my ugly code that seems to work
|
@adback03 You can't do this if you're using
|
@kassyuz You can fix it by feeding
|
I noticed some interesting behavior of echo '1' | TF_WORKSPACE=new-workspace terraform init and it automatically creates "new-workspace" on app.terraform.io without me ever running This can be demonstrated with a more complete example: terraform {
backend "remote" {
organization = "my-org"
workspaces {
prefix = "my-prefix-"
}
}
} # Assumes there aren't any workspaces on app.terraform.io with prefix "my-prefix-" yet
rm -rf ./.terraform
TF_WORKSPACE=first-workspace terraform init
# Returns Error "No existing workspaces.", expected
# We create our first workspace
terraform workspace new first-workspace
# Interestingly, when we run this, terraform automatically creates
# "my-prefix-second-workspace" on app.terraform.io
echo '1' | TF_WORKSPACE=second-workspace terraform init
# ...but doesn't select it locally
terraform workspace show
# Returns "first-workspace"
# But we can select it manually now
terraform workspace select second-workspace
# Switched to workspace "second-workspace". |
thanks @ilyasotkov I did exactly that, and it works. |
this worked for me:
|
I was able to have it working, by writing "default" to the .terraform/environment before running terraform init |
I want to apologize for the slow response time on this issue, and also let you know that I am bulk-closing all issues exclusively reported against Terraform 0.11.x, including this issue, because we are no longer investigating issues reported against Terraform 0.11.x. In most cases, when we try to reproduce issues reported against 0.11, we either can't reproduce them anymore, or the reporter has moved on, so we believe we can better support the Terraform user community by prioritizing more recent issues. Terraform 0.12 has been available since May of 2019, and there are really significant benefits to adopting it. I know that migrating from 0.11 to versions past 0.12 can require a bit of effort, but it really is worth it, and the upgrade path is pretty well understood in the community by now. 0.14 is available and stable, and we are quickly approaching an 0.15 release. We have made a significant effort in the last year to stay on top of bug reports; we have triaged almost all new bug reports within 1-2 weeks for 6+ months now. If you are still experiencing this problem, please submit a new bug report with a reproduction case that works on 0.14.x, link this old issue for context, and we will triage it. |
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. |
Terraform still produces a prompt to select a workspace when
terraform init
is run with either-input=false
orTF_IN_AUTOMATION=1
. I'm using theremote
backend.It appears there is no way presently to detect or list what workspaces are available before an
terraform init
which means that if theremote
backend is used with multi-workspaceprefix
attribute then it will always produce a blocking user input prompt. This makes automation difficult.terraform init -help
states:Which isn't being followed.
Terraform Version
Expected Behavior
Init should fail with an error message about the workspace being invalid rather than producing a prompt that will block indefinitely.
Actual Behavior
Steps to Reproduce
remote
backend configured for multi-workspace and no.terraform/environment
present or an invalid workspace specified in.terraform/environment
.Either flag will have no effect on prompt behavior.
The text was updated successfully, but these errors were encountered: