Requirement : I am trying to create a Azure Subnet into an existing VNet. Terraform IAC.
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.
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.
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}"
}
2
Answers
You can use Terraform CIDR Subnets module to split your vnet into subnets – more details here.
The only downside is you will have to define all your subnets before you actually create any of your resource. In a way, this is a good practice to define networking first.
In Terraform, dynamically retrieving an index number without manual input can be complex due to its declarative nature, which lacks the inherent ability to automatically ascertain an index based on specific conditions such as the availability of IP ranges within a virtual network.
This constraint exists because Terraform requires configurations to be defined prior to execution, limiting dynamic runtime calculations or decisions based on the states of resources created in the same execution.
To meet the requirement, to create new subnets without defining Index for IP and by picking one from available IPs you might need to utilize external scripts to read the current IP and ensure that the new IP does not conflict with an existing one.
My file structure:
main.tf(root):
variable.tf
modules/sub-net/main.tf
modules/sub-net/variable.tf
find_next_subnet_index.py
At first i have some preexisting subnet with different ranges
now when I run the command
terraform apply
the python script execute and check with the preexisting IP addresses and make a new IP which doesnt overlap with the existing one and make it available for deployment of next resource.Deployment succeeded: