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

Specified workspace "name" conflicts with TF_WORKSPACE environment variable #508

Closed
unkinected opened this issue Mar 13, 2024 · 4 comments · Fixed by #509
Closed

Specified workspace "name" conflicts with TF_WORKSPACE environment variable #508

unkinected opened this issue Mar 13, 2024 · 4 comments · Fixed by #509
Labels
bug Something isn't working upstream-terraform

Comments

@unkinected
Copy link

I'm not quite sure when this started, but all of a sudden I'm receiving an error when trying to import resources into Terraform.

I've essentially followed the Azure guide: https://learn.microsoft.com/en-us/azure/developer/terraform/azure-export-for-terraform/export-advanced-scenarios. To reproduce

  1. Run: aztfexport resource -o tempdir --non-interactive --hcl-only
  2. This successfully generates files in a new directory called "tempdir." I go through these files and tweak names to better match our naming scheme.
  3. Run: aztfexport map --non-interactive --append './tempdir/aztfexportResourceMapping.json'
  4. Error

Error: error running terraform init for the output directory: exit status 1

Error: Invalid workspaces configuration

Specified workspace "name" conflicts with TF_WORKSPACE environment variable.

The 'workspaces' block configures how Terraform CLI maps its workspaces for
this single
configuration to workspaces within a Terraform Cloud organization. Two
strategies are available:

tags - A set of tags used to select remote Terraform Cloud
workspaces to be used for this single
configuration. New workspaces will automatically be tagged with these tag
values. Generally, this
is the primary and recommended strategy to use. This option conflicts with
"name".

name - The name of a single Terraform Cloud workspace to be used
with this configuration.
When configured, only the specified workspace can be used. This option
conflicts with "tags"
and with the TF_WORKSPACE environment variable.

This has worked fine up til now, though I'll admit it has been a little while since I've needed to do this. My terraform file hasn't changed:

terraform {
cloud {
organization = "My Company"
workspaces {
name = "Azure"
}
}

I've tried setting a TF_WORKSPACE environment variable but that doesn't do anything. If I remove the workspaces block from terraform.tf I get a new error:

Error: error running terraform init for the output directory: exit status 1

Error: Invalid command-line option

The -force-copy option is for migration between state backends only, and is
not applicable when using Terraform Cloud.

State storage is handled automatically by Terraform Cloud and so the state
storage location is not configurable.

I was on an older version of aztfexport and just upgraded to 0.14.0, no change.
I was on terraform 1.6.6 and upgraded to 1.7.4, no change.

Any ideas? Generating the resource files works, but importing them into terraform state does not.

@magodo
Copy link
Collaborator

magodo commented Mar 14, 2024

@unkinected Sorry for running into this.. From the error message:

Specified workspace "name" conflicts with TF_WORKSPACE environment variable.

Looks like there is an existing env var TF_WORKSPACE in your current session, would you please check if it exists by echo $TF_WORKSPACE. if yes, then evaluate whether you shall unset it by unset TF_WORKSPACE?

@unkinected
Copy link
Author

Hi @magodo , yea, that was the first thing I checked. I do not have any environment variables named TF_WORKSPACE, and even when I do set it manually, there is no change in the output. This problem occurred on a colleague's computer too who retrieved our terraform repo fresh.

We use powershell as our cli and echo $TF_WORKSPACE and echo $env:TF_WORKSPACE both return nothing. This command doesn't work when inside the terraform console. I don't see any such variables set in the Windows env variables control panel either.

Note that I am able to add, remove, and alter my resources otherwise just fine. It's the aztfexport map command that is causing problems.

@magodo
Copy link
Collaborator

magodo commented Mar 15, 2024

Unfortunately, the environment variable TF_WORKSPACE somehow is forcely reset by the terraform-exec library: hashicorp/terraform-exec#75. There are also several upstream issues asking for that feature:

@magodo magodo added upstream-terraform bug Something isn't working labels Mar 15, 2024
@unkinected
Copy link
Author

I think you misunderstood. I don’t want to use the TF_WORKSPACE environment variable. I have never set that. I have the workspace specified in my terraform.tf file but yet aztfexport map seems to be ignoring it. Or misinterpreting it or something, thinking that there’s a conflict when there isn’t. This used to work fine and somehow stopped recently, even though my terraform.tf hasn’t changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream-terraform
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants