Azure / aztfexport

A tool to bring existing Azure resources under Terraform's management
https://azure.github.io/aztfexport/
Mozilla Public License 2.0
1.59k stars 184 forks source link

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

Closed unkinected closed 6 months ago

unkinected commented 6 months ago

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 commented 6 months ago

@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 commented 6 months ago

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 commented 6 months ago

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

unkinected commented 6 months ago

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.