OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Create subnet variable for delegation

  • Thread starter Thread starter user23350878
  • Start date Start date
U

user23350878

Guest
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.

Code:
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.

Code:
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"]
        }
      }
    }
  }
}

Code:
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
<p>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.</p>
<pre><code>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.
</code></pre>
<pre><code>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"]
}
}
}
}
}
</code></pre>
<pre><code>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"]
}
}
}
</code></pre>
<p>Create a delegated subnet for Postgres</p>
Continue reading...
 

Latest posts

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top