Hello Everyone, i am getting an error while creating virtual network gateway using the below code.
resource “azurerm_subnet” “lz_gateway_subnet” {
for_each = { for k in local.lz.Gatwaysubnet : k.Name => k}
name = “GatewaySubnet”
resource_group_name = each.value.Resource_group_name
virtual_network_name = each.value.Vnet_Name
address_prefixes = [each.value.Address_Space]
depends_on = [azurerm_virtual_network.lz_vnet]
}
resource “azurerm_local_network_gateway” “onpremise” {
for_each = { for k in local.lz.Localgateway : k.Name => k}
name = each.value.Name
location = each.value.Region
resource_group_name = each.value.Resource_group_name
gateway_address = each.value.Gateway_address
address_space = [each.value.Address_space]
}
resource “azurerm_public_ip” “lz_pubip” {
for_each = { for k in local.lz.Localgateway : k.Publicip_Name => k}
name = each.value.Publicip_Name
location = each.value.Region
resource_group_name = each.value.Resource_group_name
allocation_method = “Dynamic”
}
resource “azurerm_virtual_network_gateway” “lz_virnetworkgateway” {
for_each = { for k in local.lz.VirtualNetworkgateway : k.Name => k}
name = each.value.Name
location = each.value.Region
resource_group_name = each.value.Resource_group_name
type = each.value.Type
vpn_type = each.value.vpn_type
active_active = false
enable_bgp = false
sku = each.value.sku
ip_configuration {
public_ip_address_id = azurerm_public_ip.lz_pubip["Publicip_Name"].id
private_ip_address_allocation = "Dynamic"
subnet_id = azurerm_subnet.lz_gateway_subnet["Name"].id
}
}
Below is the Error message i am getting.
Error: Invalid index
on expressroute.tf line 62, in resource “azurerm_virtual_network_gateway” “lz_virnetworkgateway”:
62: public_ip_address_id = azurerm_public_ip.lz_pubip[“Publicip_Name”].id
|----------------
| azurerm_public_ip.lz_pubip is object with 2 attributes
The given key does not identify an element in this collection value.
Error: Invalid index
on expressroute.tf line 64, in resource “azurerm_virtual_network_gateway” “lz_virnetworkgateway”:
64: subnet_id = azurerm_subnet.lz_gateway_subnet[“Name”].id
|----------------
| azurerm_subnet.lz_gateway_subnet is object with 2 attributes
The given key does not identify an element in this collection value.
I not having any syntax error as the validation is passed, i think i have to pass the key name of publicip and gateway subnet, but not sure what is the causing the error.
I tried to create using count, it works with count.index. Any help on this topic please?
This is the input block from my json file.
“Gatwaysubnet”: [
{
"Address_Space": "172.24.50.0/27",
"Resource_group_name": "agp-sap-rg",
"Vnet_Name": "agp-hub-vnet",
"Name": "GatewaySubnet"
}
],
“Localgateway”: [
{
"Name": "agp-sap-lgw",
"Gateway_address": "168.62.225.23",
"Address_space": "10.1.1.0/24",
"Resource_group_name": "agp-sap-rg",
"Region": "Central India",
"Publicip_Name": "agp-sap-gw_pip"
}
],
“VirtualNetworkgateway”: [
{
"Name": "agp-sap-vngw",
"Type": "ExpressRoute",
"sku": "Standard",
"vpn_type": "RouteBased",
"Publicip_Name": "agp-sap-gw_pip",
"Resource_group_name": "agp-sap-rg",
"Region": "Central India"
}
]