How to set Static private ip to NIC in Azure using terraform?

Solution 1:

There isn't an easy solution to this, unfortunately. If you want a NIC with static internal IP then Azure expects you to tell it what IP you want to use, it will not pick on for you. If you look at the example for doing this with an ARM template, they are cheating and setting the IP to dynamic first, so Azure generates an IP and then changing the mode to static.

So in Terraform, you would need to do something similar, create the NIC with a dynamic IP first, then create the same NIC a second time but make it static this time.

The alternative is to use the cidrsubnet notation in Terraform to generate a valid IP address in your subnet. This will work, but the problem here is you have no guarantee that this IP is not already in use by other resources in your subnet unless you have created them all with Terraform and made sure to increment the IPs each time.