My code for Azure subnet delegation is failing. I need your help to resolve this problem, thank you. Here is by error the code is next. The error I get has something to do with the for each call in my nested block for subnet delegation. I will also accept a solution for the same task, which is to have 1 subnet variable.
Error: Invalid index
│
│ on main.tf line 26, in resource "azurerm_subnet" "subnet":
│ 26: actions = each.value["actions"]
│ ├────────────────
│ │ each.value is object with 5 attributes
│
│ The given key does not identify an element in this collection value.
variable "location" {
type = string
description = "Azure geographic location for virtual network."
}
variable "address_space" {
description = "Cidr block for virtual network."
type = list(string)
}
variable "subnets" {
description = "Public and private subnets for virtual network"
type = map(object({
name = string
address_prefixes = list(string)
service_endpoints = optional(list(string))
private_endpoint_network_policies_enabled = optional(bool)
delegation = optional(object({
name = string
service_delegation = object({
actions = list(string)
})
}))
}))
default = {
subnet1 = {
name = "tfakazurepub-vnet-pub-sub"
address_prefixes = ["192.168.0.0/29"]
}
subnet2 = {
name = "tfakazurepub-vnet-pri-sub"
address_prefixes = ["192.168.0.128/25"]
service_endpoints = ["Microsoft.Storage", "Microsoft.Keyvault"]
private_endpoint_network_policies_enabled = true
}
subnet3 = {
name = "tfakazurepub-vnet-ppsqldb-sub"
address_prefixes = ["192.168.0.16/29"]
service_endpoints = ["Microsoft.Storage"]
delegation = {
name = "pgfs"
service_delegation = {
name = "Microsoft.DBforPostgreSQL/flexibleServers"
actions = ["Microsoft.Network/virtualNetworks/subnets/join/action"]
}
}
}
}
}
resource "azurerm_subnet" "subnet" {
for_each = var.subnets
name = each.value.name
address_prefixes = each.value.address_prefixes
service_endpoints = each.value.service_endpoints
virtual_network_name = azurerm_virtual_network.virtual_network.name
resource_group_name = data.azurerm_resource_group.resource_group.name
private_endpoint_network_policies_enabled = each.value.private_endpoint_network_policies_enabled
delegation {
name = each.value.name
service_delegation {
name = each.value.name
actions = each.value["actions"]
}
}
}
Create a delegated subnet for Postgres