I have azurerm and azureapi providers that are subscription specific. E.g.,
provider "azurerm" {
alias = "first"
tenant_id = "<>"
subscription_id = var.subscription_id
skip_provider_registration = true
use_oidc = true
features {}
}
provider "azapi" {
alias = "first-api"
tenant_id = "<>"
subscription_id = var.subscription_id
use_oidc = true
}
I then also use terraform config files to change out the subscription_id.
I would like to deploy the same set of resources across multiple subscriptions in one config file but not others. That is, for dev.tfvars subscription_id is “” but in prod.tfvars it would have a value.
I’m not sure the best way to do this because if I try to use “” for the subscription id I am getting failures.
provider "azurerm" {
alias = "second"
tenant_id = "<>"
subscription_id = var.other_subscription_id <-- defaulted to "" in the variables
skip_provider_registration = true
use_oidc = true
features {}
}
provider "azapi" {
alias = "second-api"
tenant_id = "<>"
subscription_id = var.other_subscription_id
use_oidc = true
}
yields:
Error: building account: unable to configure ResourceManagerAccount: subscription ID could not be determined and was not specified
with provider["registry.terraform.io/hashicorp/azurerm"].second,
on provider.tf line 81, in provider "azurerm":
81: provider "azurerm" {
I’m considering breaking the places I use these providers out into a separate module but according to https://developer.hashicorp.com/terraform/language/modules/develop/providers all providers are global and so it won’t work.
Is there a way to make a provider optional so it will only be loaded if I need it? Or is my reading of the Docs incorrect?