Hello everybody!
Is it possible to dynamically configure the provider in a resource of a module, using for example a for_each loop?
For example:
provider.tf
provider "azurerm" {
features {}
subscription_id = "XXXXXXXXXXXX"
alias = "aliasA"
}
provider "azurerm" {
features {}
subscription_id = "XXXXXXXXXXXX"
alias = "aliasB"
}
In the module I have the following variable:
variables.tf
variable "data" {
type = list(object({
action_group = object({
create_action_group = optional(bool, false)
provider = string
resource_group_name = string
action_group_name = string
})
}))
description = "(Optional) Map of key/value to configure `Blob Storage`."
default = []
}
And into module have the follow version.tf file.
terraform {
required_version = ">= 1.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.55.0"
configuration_aliases = [ azurerm.aliasA , azurerm.aliasB ]
}
}
}
In the project main.tf
module "monitoring" {
source = "../my-module"
providers = {
azurerm.aliasA = azurerm.aliasA
azurerm.aliasB = azurerm.aliasB
}
data = [
{
...
action_group = {
provider = "aliasA"
resource_group_name = "my-rg"
action_group_name = "my-ag"
}
}
]
}
I tested the scenarios below, but all configurations tested returned me error:
data "azurerm_monitor_action_group" "default" {
for_each = var.data
provider = "azurerm.${each.value.action_group["provider"]}"
name = each.value.action_group["action_group_name"]
resource_group_name = each.value.action_group["resource_group_name"]
}
data "azurerm_monitor_action_group" "default" {
for_each = var.data
provider = azurerm(each.value.action_group["provider"])
name = each.value.action_group["action_group_name"]
resource_group_name = each.value.action_group["resource_group_name"]
}
data "azurerm_monitor_action_group" "default" {
for_each = var.data
provider = each.value.action_group.provider
name = each.value.action_group["action_group_name"]
resource_group_name = each.value.action_group["resource_group_name"]
}