How to assign Static IP to Azure VM

Posted by in Azure

When you start working with Windows Azure IaaS offering, you quickly realize that the age old ways of dealing with infrastructure configuration immediately becomes invalid. Let me give you an example: when you setup a new server in your datacenter, one of the first things you do is acquire an IP address from your network. It doesn’t matter how, maybe just a simple ping test to see which IP is available, maybe from that spreadsheet you have to keep track of used and usable IP addresses, or an IPAM deployed on your network. Well, in Windows Azure, you can’t do that. You see, Windows Azure has to know which IP it has allocated to which VM. It’s only when Windows Azure allocates an IP address to a VM, it actually becomes routable IP in Azure fabric. You can’t reach an IP you decided to assign to a VM yourself!

This creates a great challenge when you have to shut down a group of VMs for one of many reasons. If you have created virtual network within Windows Azure, you atleast have a workaround. Start VMs in same order every time (i.e. DC, ADFS, SharePoint and so on). As long as you maintain that order every time, you will notice that IP addresses are allocated in same order from your associated virtual network. However, this poses a great challenge. You must strictly adhere to a routine and that may add more time to respond to a requirement to bring a service online quickly.

Well, not anymore! With February 2014 release of Windows Azure PowerShell, new cmdlets are available that will allow you to manage static allocation of IP addresses from your virtual network in Windows Azure. I am having very hard time finding related article from Windows Azure team that talks about this feature. If I was them, I would be shouting it from rooftops!

For you detail oriented minds, the Windows Azure PowerShell version should be 0.7.3.1 or higher (yes March 2014 update is already available and version reads 0.7.4).

Need to know how to install Windows Azure PowerShell? Go here: http://www.windowsazure.com/en-us/documentation/articles/install-configure-powershell/

Anyhow, the cmdlets are as follows:

Get-AzureStaticVNetIP
Remove-AzureStaticVNetIP
Set-AzureStaticVNetIP
Test-AzureStaticVNetIP

So let me show you how you can actually allocate a static IP address to a VM in Azure. First things first, make sure you have a virtual network (VNet) created. For Example, I have created a VNet called “tenzero” and have created a subnet… “10.0.0.0/24”.

image

I have noticed that Azure always assigns IP addresses starting at 4th usable IP of the range. So 10.0.0.4 should be first available IP I should be able to assign. That is assuming I don’t have any VM’s created in that VNet. In  my setup, I have a VM conveniently named “TestStaticIP”. Given logic we discussed, Azure should have allocated 10.0.0.4 to that VM already. Let’s check:

PS C:\> Test-AzureStaticVNetIP -VNetName TenZero -IPAddress 10.0.0.4

IsAvailable          : False
AvailableAddresses   : {10.0.0.5, 10.0.0.6, 10.0.0.7, 10.0.0.8…}
OperationDescription : Test-AzureStaticVNetIP
OperationId          : f6bbba4a-ecf6-70c1-ac41-fc89a1f62b02
OperationStatus      : Succeeded

Notice that IP Address 10.0.0.4 is not available: “IsAvailable: False”. Now, let’s check the VM and make sure it has 10.0.0.4 assigned by DHCP (only relevant items shown):

PS C:\Users\bshukla> ipconfig /all

Windows IP Configuration

   Host Name . . . . . . . . . . . . : TestStaticIP

Ethernet adapter Ethernet 2:

   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   IPv4 Address. . . . . . . . . . . : 10.0.0.4(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : Thursday, March 13, 2014 3:30:33 PM
   Lease Expires . . . . . . . . . . : Sunday, April 19, 2150 10:09:06 PM
   Default Gateway . . . . . . . . . : 10.0.0.1
   DHCP Server . . . . . . . . . . . : 168.63.129.16

Just as expected. So we can’t assign 10.0.0.4 to the VM. We would have been able to if we assigned it on creation. Niall Moran has described that process well on his blog so I will skip the repetition. If you notice in output of Test-AzureStaticVNerIP, it shows available addresses as a suggestion. Since 10.0.0.5 is one of them, let’s allocate it for our VM. Notice that I am using pipeline because “VM” is a required parameter but it expects “Persistent VM Object” and not a string containing VM name. I figured easiest way for me is to pass it via pipeline since it accepts object from pipeline:

PS C:\> get-help Set-AzureStaticVNetIP -Parameter VM

-VM <PersistentVM>
    Persistent VM object.

    Required?                    true
    Position?                    1
    Default value
    Accept pipeline input?       true (ByValue)
    Accept wildcard characters?  false

PS C:\> Get-AzureVM -Name TestStaticIP -ServiceName TestStaticIP | Set-AzureStaticVNetIP -IPAddress 10.0.0.5 | Update-AzureVM

Update-AzureVM is important as the changes won’t be committed without it. It’s also important to note that when you run the cmdlets mentioned above including Update-AzureVM, it will restart your VM so be prepared to lose connectivity and if it is in production plan ahead!

With all that said and done, let the VM start and you will have VM running with your allocated “Static” IP! Remember, nothing changes inside VM. It is still a DHCP configuration, like it was before, but Azure knows not to assign anything but allocated “Static” IP to it. Enjoy!