Open bonnnk opened 1 year ago
I'm encountering the same problem with azurerm_virtual_network and azurerm_subnet resources. It seems the flatten function causes a "container" resource to require all of a"tenant" resource's arguments.
│ Error: Incorrect attribute value type
│
│ on modules/network/subnet/vnet.tf line 24, in resource "azurerm_virtual_network" "vnet":
│ 24: subnet = [ for key, value in each.value.subnets : azurerm_subnet.subnet[key] ]
│ ├────────────────
│ │ azurerm_subnet.subnet is object with 1 attribute "gateway_subnet"
│ │ each.value.subnets is map of object with 1 element
│
│ Inappropriate value for attribute "subnet": element 0: attributes "address_prefix" and "security_group" are required.
Note the arguments listed in the error message: "address_prefix" and "security_group". I shouldn't be receiving these errors since I'm not calling azurerm_virtual_network embedded subnet functionality.
For azurerm_virtual_network embedded subnet blocks:
For azurerm_subnet:
variable "vnets" {
type = map(object({
vnet_name = string
vnet_address_space = list(string)
subnets = map(object({
subnet_name = string
subnet_vnet_name = string
subnet_address_prefixes = list(string)
}))
}))
}
locals {
all_subnets = flatten([
for vnet_key, vnet_value in var.vnets : [
for subnet_key, subnet_value in vnet_value.subnets : {
vnet_key = vnet_key
vnet_name = vnet_value["vnet_name"]
subnet_key = subnet_key
subnet_name = subnet_value["subnet_name"]
subnet_address_prefixes = subnet_value["subnet_address_prefixes"]
}
]
])
}
resource "azurerm_virtual_network" "vnet" {
for_each = var.vnets
resource_group_name = var.resource_group_name
location = var.location
name = each.value.vnet_name
address_space = each.value.vnet_address_space
subnet = [ for key, value in each.value.subnets : azurerm_subnet.subnet[key] ]
}
resource "azurerm_subnet" "subnet" {
for_each = {
for subnet in local.all_subnets : subnet.subnet_key => subnet
}
resource_group_name = var.resource_group_name
name = each.value.subnet_name
virtual_network_name = each.value.vnet_name
address_prefixes = each.value.subnet_address_prefixes
}
variable "vnets" {
type = map(object({
vnet_name = string
vnet_address_space = list(string)
subnets = map(object({
subnet_name = string
subnet_vnet_name = string
subnet_address_prefixes = list(string)
}))
}))
}
module "subnet" {
source = "./modules/network/subnet"
resource_group_name = data.azurerm_resource_group.poc-networkautomation-centralus-rg.name
location = data.azurerm_resource_group.poc-networkautomation-centralus-rg.location
vnets = var.vnets
}
vnets = {
my-test-vnet = {
vnet_name = "my-test-vnet"
vnet_address_space = ["10.2.0.0/24", "10.3.0.0/24"]
subnets = {
gateway_subnet = {
subnet_name = "GatewaySubnet"
subnet_vnet_name = "my-test-vnet"
subnet_address_prefixes = ["10.2.0.0/27"]
}
}
}
}
Is there an existing issue for this?
Community Note
Terraform Version
Seen in 1.2.6 and 1.3.4
AzureRM Provider Version
Seen in 3.18 and 3.30.0
Affected Resource(s)/Data Source(s)
azurerm_route
Terraform Configuration Files
Module layout:
Child Module:
variables.tf
route_table.tf
Parent Module:
variables.tf
main.tf
eastUS.tfvars
Debug Output/Panic Output
Expected Behaviour
Route entry "test_route_entry" is created and added to route table "my-route-table-name".
Plan Output
Actual Behaviour
Steps to Reproduce
Important Factoids
"next_hop_in_ip_address" correctly interprets "null" when azurerm_route is outside of a flattened for_each loop.
References
No response