Requirement : I am trying to create a Azure Subnet into an existing VNet.
Issue : I am unable to dynamically get the available IP ranges in the VNet’s address space and assign one available IP to Subnet’s address_prefixes variable.
Trials : I tried using [cidrsubnet(<VNet’s Address Space>, ,netnum, <no. of subents to be created>)
module "subnet"{
source = "./modules/sub-net"
name = "${var.environment}${replace(var.servicename,"-","")}Subnet"
resource_group_name = data.azurerm_resource_group.existing.name
virtual_network_name = data.azurerm_virtual_network.existing_vnet.name
address_prefixes= cidrsubnet(data.azurerm_virtual_network.existing_vnet.address_space[0], 4, 2)]
network_security_group_id = "${var.network_security_group_id}"
route_table_id = "${var.route_table_id}
}
But this gives an IP that is already in use, it does not validates the availability of the IP and so terraform apply throws an IP overlap error.
Last option used is to harcode the available IP address as value to address_prefixes but this is not a good standard to follow especially if we are creating for multiple environments and so on.
module "subnet"{
source = "./modules/sub-net"
name = "${var.environment}${replace(var.servicename,"-","")}Subnet"
resource_group_name = data.azurerm_resource_group.existing.name
virtual_network_name = data.azurerm_virtual_network.existing_vnet.name
address_prefixes = ["1XX.20.1XX.2XX/28"]
network_security_group_id = "${var.network_security_group_id}"
route_table_id = "${var.route_table_id}"
}
Question : Is there any way to first get the available IP ranges in a VNet address space and then assign one of them to SubNet’s address prefixes.