Search code examples
azureterraformterraform-provider-azure

loop through terraform provider block for multiple subscription


I want to deploy 20 azure resource group in 20 different subscriptions. is there any way i can achieve this with minimum code.

i am using below code.

provider "azurerm" {
    alias = "dev"
    subscription_id = var.network_services_rgs.value.subs
}

resource "azurerm_resource_group" "rg" {
  for_each          = var.network_services_rgs
  provider          = azurerm.dev
  name              = each.key
  location          = each.value.location
  tags              = lookup(each.value, "tags", null)
}

this is for just one RG but how can i extent this to support multiple subscription. i do not want to have 20 providers block for each subscription. please help.


Solution

  • You need to write 20 provider blocks to use it in Resource group block as you need to mention the provider alias.