Using for_each expression in Terraform v0.12.6 to dynamically generate inline blocks of vnet subnets (Azure). I have list variable 'subnets' defined, with two subnets 'sub1' and 'sub2' as below
variable "subnets" {
default = [
{
name = "sub1"
prefix = "1.1.1.1/32"
},
{
name = "sub2"
prefix = "2.2.2.2/32"
},
]
}
then iterate over list variable inside "azurerm_virtual_network" block to create dynamic blocks of subnets
dynamic "subnet" {
for_each = [for s in var.subnets : {
name = s.name
prefix = s.prefix
}]
content {
name = subnet.name
address_prefix = subnet.prefix
}
}
}
Getting i.e. first one is Error: Unsupported attribute
on main.tf line 42, in resource "azurerm_virtual_network" "vnet": 42: name = subnet.name
This object does not have an attribute named "name".
for s in var.subnets
should work for iteration without thatfor_each
. – Matt Schuchard