Open t-l-k opened 4 years ago
Looking at this, seems like this might be three issues in one:
az cli
. Presumably, from reading between the lines of the documentation and your report, they default to using current logged-in creds in az
(ie, ignoring the tenant_id
and subscription_id
fields), but this isn’t spelled out.tenant_id
and subscription_id
to override current az cli
creds.ARM_TENANT_ID
and ARM_SUBSCRIPTION_ID
variables does not behave the same way as setting tenant_id
and subscription_id
in the config.Is that a fair summary/disambiguation?
@apmarshall Yes, a fair summary indeed.
3. **A consistency issue**: apparently using the `ARM_TENANT_ID` and `ARM_SUBSCRIPTION_ID` variables does not behave the same way as setting `tenant_id` and `subscription_id` in the config.
If those values are set as environment variables then they do take effect. But backend tenant_id
and subscription_id
are ignored regardless of whether the environment variables are set.
To be honest, if those ARM_*
environment variables are set, I would expect them to override anything set in the backend configuration anyway, so that appears to work? Although maybe terraform should write out a warning?
Specifying tenant_id
and subscription_id
with Azure CLI authentication appears to work now. I'm deploying resources to the default subscription as selected in the CLI, but the state is stored in a separate subscription set via the backend config. Can anyone confirm if this issue is fixed? Version information:
$ az version
{
"azure-cli": "2.21.0",
"azure-cli-core": "2.21.0",
"azure-cli-telemetry": "1.0.6",
"extensions": {}
}
$ tf version
Terraform v0.14.9
+ provider registry.terraform.io/hashicorp/azurerm v2.54.0
@mxk it's still exhibiting the odd behaviour for me. It's attempting to use the subscription from az account show
instead of the one annotated in the backend's subscription_id
.
az-cli v2.23.0
azurerm v2.58.0
terraform v0.14.9
It seems to work on azurerm v2.68.0, although I don't feel confident enough yet to confirm.
Still an issue in v2.86.0
Still an issue in azurerm v3.32.0
@t-l-k To be honest, if those ARM_* environment variables are set, I would expect them to override anything set in the backend configuration anyway, so that appears to work? Although maybe terraform should write out a warning?
I tend to disagree. Configuration in files should always get preference above environment variables.
@t-l-k To be honest, if those ARM_* environment variables are set, I would expect them to override anything set in the backend configuration anyway, so that appears to work? Although maybe terraform should write out a warning?
I tend to disagree. Configuration in files should always get preference above environment variables.
It's literally the other way around in other projects, ansible comes to my mind first where CLI arguments override all. If you're that specific to input CLI arguments - I'd be mad if they were overriden by some spaghetti inside of whatever i'm running
Terraform Version
Terraform Configuration Files
Debug Output
Expected Behavior
It should use the
tenant_id
andsubscription_id
as configured in the backend.The documentation appears to indicate that
tenant_id
andsubscription_id
are exclusively use for the following scenarios:But honestly, I expect it also to work when using Azure CLI, to authenticate against the tenant and subscription I have explicitly stated. I don't want to authenticate using a SPN, or certificate or etc, I just want to use Azure CLI for authentication, to authenticate as me.
Actual Behavior
It appears to just use what ever ambient Azure CLI account I have logged in.
I have multiple
az account
contexts, across multiple tenants and subscriptions. It is not always convenient to continuously switch my active one, when the correct account authentication context should be resolved via the explicitly set (and unchanging)tenant_id
andsubscription_id
in the backend configuration.HOWEVER, I tried also setting
ARM_SUBSCRIPTION_ID
andARM_TENANT_ID
environment variables. To my surprise, this works! It does use the values from those environment variables when performing Azure CLI authentication!So the documentation is also a bit inconsistent, because it also implies those variables are also exclusively for use by the 3 scenarios under Expected Behaviour.
I'd rather it just used the settings from the backend configuration.
Steps to Reproduce
1) Have multiple tenants & subscriptions authenticated with
az
the Azure CLI, plus also a storage account for some remote state etc. 2)az account set
with the terraform subscription you intend to use. 3) Settenant_id
andsubscription_id
in the backend configuration to match the storage account from (1) and the account selected in (2). 4) Write some state into the backend, e.g.terraform apply
! 5)az account set
to a tenant & subscription which is not the same as configured for the backend. 6)terraform init
7) Kabooom!