I have this EXACT same issue to the letter: Trouble passing an aliased provider to a module · Issue #35795 · hashicorp/terraform · GitHub
Has this been solved? I can’t find anything on it. Providers Within Modules - Configuration Language | Terraform | HashiCorp Developer
I tried to post my TF code, but this forum says I can only post 2 links as a new user. So I can try to post more tf code to prove my code is right if requested.
Hi @vexter0944,
We need to see the configuration and the exact error message in order to determine what is really going on. Rather than linking to your configuration, creating a minimal, reproducible example and formatting in your post would be more illustrative.
Thanks @jbardin - I’ve redacted and renamed some items - but I think the idea is there. Let me know if you need more info etc. Happy to respond back as needed. I did reach out to the original poster from the github link above - he had also prosted on Reddit - he responded he never did get a fix. Reddit - The heart of the internet
root
---------------
main.tf
terraform {
required_version = ">= 1.8.5, < 2.0.0"
required_providers {
azurerm = {
version = ">= 4.0.0, < 5.0.0"
source = "hashicorp/azurerm"
}
}
}
provider "azurerm" {
alias = "mgmt"
tenant_id = "redacted"
subscription_id = "redacted"
client_id = "redacted"
features {}
resource_provider_registrations = "none"
}
provider "azurerm" {
alias = "prod"
tenant_id = "redacted"
subscription_id = "redacted"
client_id = "redacted" features {}
resource_provider_registrations = "none"
}
module "resource_group" {
for_each = var.map_rgs
providers = {
azurerm = each.value.azurerm_alias == "mgmt" ? azurerm.mgmt : azurerm.prod
#azurerm.mgmt = azurerm.mgmt
#azurerm.prod = azurerm.prod
}
source = "./modules/resource_group"
module_azurerm_resource_group_name = each.value.rg_name
module_azurerm_resource_group_location = each.value.rg_location
module_azurerm_provider_alias = each.value.azurerm_alias
}
tf.auto.tfvars
map_rgs = {
rg_sub1 = {
rg_name = "rg_sub1"
rg_location = "East US 2"
azurerm_alias = "mgmt"
}
rg_sub2 = {
rg_name = "rg_sub2"
rg_location = "East US 2"
azurerm_alias = "prod"
}
}
variables.tf
variable "map_rgs" {
type = map(any)
}
-----------
resource_group module (./modules/resource_group)
main.tf
terraform {
required_providers {
azurerm = {
version = ">= 4.0.0, < 5.0.0"
source = "hashicorp/azurerm"
#configuration_aliases = [azurerm.mgmt, azurerm.prod]
}
}
}
resource "azurerm_resource_group" "rg" {
name = var.module_azurerm_resource_group_name
location = var.module_azurerm_resource_group_location
}
outputs.tf
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
output "resource_group_location" {
value = azurerm_resource_group.rg.location
}
variables.tf
variable "module_azurerm_resource_group_name" {
}
variable "module_azurerm_resource_group_location" {
}
variable "module_azurerm_provider_alias" {
}
@jbardin - also jic: The Terraform error. You can see in my main.tf above - the feature{} setting is there. But it does not appear to hand it into the module even if aliased.
Planning failed. Terraform encountered an error while generating this plan.
╷
│ Error: Invalid provider configuration
│
│ Provider “Terraform Registry” requires explicit
│ configuration. Add a provider block to the root module and configure the
│ provider’s required arguments as described in the provider documentation.
│
╵
╷
│ Error: Missing required argument
│
│ with provider[“Terraform Registry”],
│ on line 0:
│ (source code not available)
│
│ The argument “features” is required, but no definition was found.
╵
::error::Terraform exited with code 1.
Error: Process completed with exit code 1.
Please format the posts as plain text so that I can copy&paste exactly verbatim and run against terraform. From what I can make out in the example, there is no reason for Terraform to return an error about a missing configuration.
You seem to have copy&pasted at least some of the invalid formatting I was trying to avoid in the original. You should be able to edit your previous posts, and format the original text.
@jbardin - edited original post. Let me know if it needs anything else.
Sorry, the configuration still has mismatched provider names, but those are completely invalid so I don’t think you would get to a plan at all. If I edit all the names so they match, terraform will successfully start a plan. I would suggest taking the config you are showing and trying it yourself to make sure it’s both valid, and actually demonstrates the issue you are trying to communicate.
@jbardin - ok put some new code up at the top.
The expectation is that I want to create a azurerm res grp in prod and mgmt.
I want to use a for_each with a map call map_rgs
map_rgs has the subscription I want to create each RG in.
Right now please note these 2 areas:
module "resource_group" {
for_each = var.map_rgs
providers = {
azurerm = each.value.azurerm_alias == "mgmt" ? azurerm.mgmt : azurerm.prod
#azurerm.mgmt = azurerm.mgmt
#azurerm.prod = azurerm.prod
}
and
terraform {
required_providers {
azurerm = {
version = ">= 4.0.0, < 5.0.0"
source = "hashicorp/azurerm"
#configuration_aliases = [azurerm.mgmt, azurerm.prod]
}
In the first block I try to work with a ternary and get an error of
PS C:\Temp\tf_module_multi_provider> terraform init
Initializing the backend...
Initializing modules...
╷
│ Error: Invalid provider configuration reference
│
│ on main.tf line 34, in module "resource_group":
│ 34: azurerm = each.value.azurerm_alias == "mgmt" ? azurerm.mgmt : azurerm.prod
│
│ The providers argument requires a provider type name, optionally followed by a period and then a configuration alias.
I've tried having the ternary in the main.tf of root and the ternary in main.tf of the module - but it won't recoginize the providers when a ternary is used. Which is 'slightly' better than where this thread started, but I don't see a way to use ternarys etc to choose providers.
So this is very different from the original error and issue you linked to. Providers need to be statically defined in the configuration, you can’t dynamically assign them like this. The assignment here is a conditional expression, but the providers need to be assigned to their respective resources before the expression can be evaluated.