Is it possible to configure the provider in a module dynamically?

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"]
}
1 Like

No, it is not possible, Terraform does not implement dynamic references to providers.

The closest you could get would be to write out a separate data block for each provider, and use filtering logic within the for_each values, so that some list entries are processed by one data block and others by the other.

Hi @maxb

Thanks for you response, it was exactly I thougth, but I didn’t have sure.

I will remove the resource data from the module and put It on the project and so passing th output for my variable.

@maxb
I am also facing same issue. Using the same example as given in this query.

What if the provider configuration is not known, e.g. region is unkown and region is an input to the terraform script. In this case, we will not be able to create multiple data block based for each provider.

Could you please help on how this can be resolved using terraform?

Thank you in advance.

If the issue is the same, so is my answer.

But that does not sound the same.

It is generally a lot less confusing if you start a new topic, and fully describe your own issue - rather than adding on to the end of someone else’s that is partially related but not the same.

You haven’t given any example code, so I’m not sure exactly what your situation is:

  1. If you just have one provider instance, and want to set the region in its configuration from a Terraform variable, that’s fine.

  2. If you want to dynamically configure a varying number of provider instances for different regions based on input variables… this is totally utterly not supported.

If you have any substantial follow-up, I encourage you to do it by starting a new topic and explaining your use case in more detail.

1 Like

Apologies , I am new to this forum.
I have created a separate issue.
Thank you for the support.