Provider Error Using module path

I have a provider I’m using with an alias for AzureRM creation which has an module output for subscription ID. I’m getting the error below. I’m also using a tfvars file that has a map variable for prod-sub that has a string value. I think I understand that terraform doesn’t know the subscription ID because of the it hasn’t actually created the subscription yet but I do have an output file in the module that should spit out the subscription id. Any idea why it can’t read the id into the provider and use to complete the configuration?

building account: unable to configure ResourceManagerAccount: subscription ID could not be determined and was not specified

#provider block
provider “azurerm” {
alias = “prod-sub”
client_id = var.sp_id
client_secret = var.sp_secret
subscription_id = module.subscriptions_prod[“prod-sub”].subscription_id
tenant_id = var.tenant_id
features {}

Terraform operations are strictly split into plan and apply phases.

A consequence of this is that you CANNOT use any data that doesn’t exist until the apply has finished, to configure providers - because the providers need to be configured to make the plan.

i.e. you can’t create a subscription in the same Terraform configuration as a provider that will use that subscription.

You need to break the configuration up into multiple Terraform configurations, so that one can be planned and applied, and then only after that has happened, the next Terraform configuration can be planned and applied.

1 Like

Thanks Max! That’s kind of what I figured but thought maybe the state would have it by output and it could be referenced in another TF file. In this case network peering during the same run.

That would require a sort of continuous feedback system in which Terraform did a bit of planning, a bit of applying, then a bit more planning then a bit more applying - but that’s just not how the tool is architected.

This issue is recreated logging into azure using environment variables described here: Terraform Registry

However if one utilize logging into azure using azcli with the exact same credentials terraform is able to plan using a provider with a subscription guid that dosent exist yet:

1 Like