Setting static IPs for Azure VMs Using Terraform Cidrhost

I came across a requirement where I needed to apply static IPs to the network interfaces applied to a couple of domain controllers using terraform. The cause I was using the count parameter to build the DC’s and the interfaces, so I was asking Terraform to build two servers and the corresponding netowk interfaces from the same resource elements. I used the count parameter to ask for two of the VM and Interface the same please with a little interpolation to get the names to use count.index to produce unique names e.g. dc01-vm dc02-vm etc.

So getting the code to build with dynamic IP’s was fairly straightforward but as two of the servers were this brings a concern a danger building Domain Controller’s. Building the two servers with dynamic IPs, we know will work, but static is better. I needed to make sure that the IP would not change for the servers. trick to it was how to do this. I found that if the “private_ip_address_allocation” parameter on the Network Interface is set to Static, terraform then requires that the “private_ip_address” parameter is supplied with an appropriate IP address. This is all well and good if the resource block for the interfaces is being done individually but if count is being used how do you do that. I have only shown the IP configuration block for each of the examples

example if being built individually
ip_configuration {
name = “internal”
subnet_id = azurerm_subnet.main_subnet.id
private_ip_address_allocation = “Static”
private_ip_address = “192.168.0.10”
}

I did the usually googling and came across a Terraform function called cidrhost, it is part of a suite of 3 functions that deal with IP Network functions. This function requires two parameters to be supplied a subnet and a hostnum, it then calculates the IP in the subnet against the hostnum, the IP usually equates to the hostnum so the code changes to look like this

example using cidrhost
ip_configuration {
name = “internal”
subnet_id = azurerm_subnet.main_subnet.id
private_ip_address_allocation = “Static”
private_ip_address = cidrhost (“192.168.0.0/24″, 16”)
}

The IP from the above code would be 192.168.0.16/24, I tested this..and unsurprisingly it worked a treat. So the next question was could I pass it variables and the count index so I changed the code again and added the count into the mix.

example using variables
ip_configuration {
name = “internal”
subnet_id = azurerm_subnet.main_subnet.id
private_ip_address_allocation = “Static”
private_ip_address = cidrhost (var.subnet_address_prefix, count.index + 10)
}

The variable “subnet_address_prefix” value was “192.168.0.0/24”, the count.index value was 0 (which was the first first interface) and the Static IP created was 192.168.0.10, the second interface was .11, I tried changing the count quite high and all worked as it should. So I incorporated this into my main project, now I can dictate the IP within the subnet for the Domain Controllers and pretty much any server.

To demonstrate this I have created a repository with a little Terraform Project in it. Its easy to play with the variable to change the host IP

The Repository is at https://github.com/RobinSnelson/terraform_cidrhosthttps://github.com/RobinSnelson/terraform_cidrhost

As usual any feedback will be gratefully received. Any questions please ask

Leave a Reply

Your email address will not be published. Required fields are marked *