Hi, I created a virtual network in azure with many subnets.
Afterward, in another module, I want to associate the relevant subnet to a resource, but I can’t find a way to get the subnet ID from the azurerm_virtual_network by using the subnet name(i.e.: module.network.subnets[subnetX].id).
It seems I can only use pointer to a list element(like subnetResourceId = module.network.subnets[5].id) The problem with that is obvious. If someone adds or remove a subnet, it will potentially break the code.
Is there anyway to point to the right subnet without chances of errors?
Here is the code for the virutal netowrk:
resource "azurerm_virtual_network" "networkVnet" {
name = var.vnetName
location = var.location
resource_group_name = var.rg
tags = var.tags
address_space = [var.vnetCIDR]
subnet {
name = "AzureFirewallSubnet"
address_prefix = var.firewallCIDR
}
subnet {
name = "GatewaySubnet"
address_prefix = var.gatewayCIDR
}
subnet {
name = "PrivateEndpointsSubnet"
address_prefix = var.privateEPCIDR
}
subnet {
name = "AzureBastionSubnet"
address_prefix = var.bastionCIDR
}
subnet {
name = "DNSSubnet"
address_prefix = var.dnsCIDR
}
subnet {
name = "JumpboxSubnet"
address_prefix = var.jumbpboxCIDR
security_group = azurerm_network_security_group.networkNSG.id
}
}