Creating a resource via a for_each

Hey all

To save having to do a lot of copy and paste code for each subnet in an Azure VNet I want to deploy I’m hoping I can use a single ‘azurerm_subnet’ resource and loop round a variable to populate values such as name and range.

I have read a few posts on the forum and have come up with this but it’s still not working. Am I a million miles off or missing something small and obvious?

In my main.tf I have:

> resource "azurerm_subnet" "subnet" {
> for_each = { for n in var.subnets : n.name => n.range }
>   name                 = upper("SN-${var.subnets[each.key].name}")
>   resource_group_name  = azurerm_resource_group.rg_networking.name
>   virtual_network_name = module.prod_virtual_network.vnet_name
>   address_prefixes     = "${var.subnets[each.key].range}"
> }

In my variables.tf I have:

variable "subnets" {
  type = list(object({
    name = string
    range  = list(string)
  }))
  default = [
    { name = "subnet1", range = ["10.0.1.0/24"] },
    { name = "subnet2", range = ["10.0.2.0/24"] },
  ]
}

The error I’m getting when I run a plan is:

│ Error: Invalid index

│ on main.tf line 47, in resource “azurerm_subnet” “subnet”:
│ 47: name = upper(“SN-{var.subnets[each.key].name}") │ ├──────────────── │ │ each.key is "subnet2" │ │ var.subnets is list of object with 2 elements │ │ The given key does not identify an element in this collection value: a number is required. ╵ ╷ │ Error: Invalid index │ │ on main.tf line 47, in resource "azurerm_subnet" "subnet": │ 47: name = upper("SN-{var.subnets[each.key].name}”)
│ ├────────────────
│ │ each.key is “subnet1”
│ │ var.subnets is list of object with 2 elements

│ The given key does not identify an element in this collection value: a number is required.


│ Error: Invalid index

│ on main.tf line 50, in resource “azurerm_subnet” “subnet”:
│ 50: address_prefixes = “{var.subnets[each.key].range}" │ ├──────────────── │ │ each.key is "subnet2" │ │ var.subnets is list of object with 2 elements │ │ The given key does not identify an element in this collection value: a number is required. ╵ ╷ │ Error: Invalid index │ │ on main.tf line 50, in resource "azurerm_subnet" "subnet": │ 50: address_prefixes = "{var.subnets[each.key].range}”
│ ├────────────────
│ │ each.key is “subnet1”
│ │ var.subnets is list of object with 2 elements

│ The given key does not identify an element in this collection value: a number is required.

Any help would be massively appreciated!

Thanks :slight_smile: