Mark
Mark

Reputation: 107

Terraform - Loop subscription creation and resource group creation in the same module

I have a module that needs to create an azure subscription then add add resource groups (and other resources) to it.

My understanding is the azurerm provider is created in the init stage, which happens before the subscription is created. You can't dynamically create providers and you can't update the subscription_id in the provider configuration using a loop.

So in this annoying situation, how would you add a list of resource_groups to a list of subscriptions in the same module?

locals {
  subscriptions = ["example1", "example2"]
  resource_groups = ["rg1", "rg2"]
}

resource "azurerm_subscription" "example" {
  for_each = local.subscriptions
  subscription_name = each.value.name
}

resource "azurerm_resource_group" "example" {
  for_each = local.resource_groups
  name     = each.value.name
  location = "West Europe"
}

Upvotes: 0

Views: 881

Answers (1)

Jahnavi
Jahnavi

Reputation: 8048

Firstly, in this case, you can use the depends_on block to set up a dependency between the azurerm_subscription & azurerm_resource_group. It will create the subscription resource prior based on the dependency you provide.

Or you can use the below script to achieve it as detailed below.

Due to some restriction issues, I tried to replicate the same in my environment by creating multiple resource groups and resources instead of subscriptions and it worked as follows.

Note: You can modify the below template for subscription and resource group resources as per the requirement.

provider "azurerm" {
features{}
}
variable "resource_groups" {
  type = list
  default = ["Rg1","Rg2"]
 }

 variable "Storage_accounts" {
  type = list
  default = [{ name = "sabla1", location = "eastus" }, {name = "sable2", location = "eastus" }]
 }
 
 locals {
  flat_list = setproduct(range(length(var.resource_groups)), var.Storage_accounts)
}

resource "azurerm_resource_group" "example"{
count                = length(var.resource_groups)
name                 = var.resource_groups[count.index]
location =  "eastus"
}

resource "azurerm_storage_account" "storage_account" {
  count                 = length(local.flat_list)
  name                  = local.flat_list[count.index][1].name
  resource_group_name  = azurerm_resource_group.example[local.flat_list[count.index][0]].name
  location             = local.flat_list[count.index][1].location
  account_tier         = "Standard"
  account_replication_type = " "
}

Once the terraform is initialized and validated successfully, executed terraform plan:

enter image description here

Executed terraform apply:

enter image description here

Deployed successfully:

enter image description here

For subscriptions & resource groups try below code template:

variable "resource_groups" {
  type = list
  default = [{name = "Rg1", location = "eastus"}, {name = "Rg2", location = "eastus"}]
 }
 variable "subscriptions" {
  type = list
  default = ["sub1", "sub2"]
 }
  locals {
  flat_list = setproduct(range(length(var.subscriptions)), var.resource_groups)
}
resource "azurerm_subscription" "example"{
count                = length(var.subscriptions)
name                 = var.subscriptions[count.index]
....
}
resource "azurerm_resource_group" "example" {
  count                 = length(local.flat_list)
  name                  = local.flat_list[count.index][1].name
  location             = local.flat_list[count.index][1].location
}

Refer azurerm_subscription in terraform registry for all the required fields.

Upvotes: 1

Related Questions