Q: How do I set a static IP address for a virtual machine (VM) in Windows Azure IaaS?

A: Prior to Windows Azure PowerShell 0.7.3.1, it wasn't possible to give a VM in a virtual network subnet a static IP address that persisted even if the VM was deprovisioned (shutdown from the portal) and therefore no long billed for.

The only option was to create different virtual subnets for different groups of VMs to try and control the IP address the VM would be given from Windows Azure.

RELATED: Video: Using Static IP Addresses in Azure

The Virtual Network static IP feature is now available through PowerShell (0.7.3.1 and later), which is available at the Window Azure website. This enables a static IP to be assigned to a VM that will persist with the VM, even if it's deprovisioned. The VM must be within a virtual network to have a static IP address configured. There are four cmdlets used:

  • Set-AzureStaticVNetIP - Sets a static IP address, e.g. Set-AzureStaticVNetIP -VM <VM> -IPAddress <IP address string>
  • Get-AzureStaticVNetIP - Gets the static IP address of the passed VM
  • Remove-AzureStaticVNetIP - Removes the static IP address of the passed VM
  • Test-AzureStaticVNetIP - Checks to see if a certain static IP address is available from a virtual network, e.g., Test-AzureStaticVNetIP -VNetName <network name> -IPAddress <IP address string>

As a best practice, don't mix VMs using static IP addresses with VMs using DHCP within a subnet. Instead, create dedicated subnets within the virtual network that will be used for VMs using static IP addresses. Below is an example of these cmdlets in action in my environment, but this won't actually take effect until the Windows Azure region you're using has implemented this functionality:

PS C:\> Test-AzureStaticVNetIP -VNetName SavTechNet115 -IPAddress 10.7.115.51

IsAvailable          : False
AvailableAddresses   : {10.7.115.52, 10.7.115.53, 10.7.115.54, 10.7.115.55...}
OperationDescription : Test-AzureStaticVNetIP
OperationId          : c6f70cc4-bf36-7907-9c7e-bc1c2e925cbb
OperationStatus      : Succeeded


PS C:\> Test-AzureStaticVNetIP -VNetName SavTechNet115 -IPAddress 10.7.115.55


IsAvailable          : True
AvailableAddresses   : {}
OperationDescription : Test-AzureStaticVNetIP
OperationId          : 30d13740-42a3-724a-8dc8-0f418a6a6af9
OperationStatus      : Succeeded


PS C:\> $staticVM = Get-AzureVM -ServiceName savnetazu115 -Name TestStaticVM

PS C:\> Set-AzureStaticVNetIP -VM $staticVM -IPAddress 10.7.115.55 | Update-AzureVM

PS C:\> Get-AzureStaticVNetIP -VM $staticVM

IPAddress                                                                                                                                                                                    
---------                                                                                                                                                                                    
10.7.115.55  

I walk through this whole process in my video Using Static IP Addresses in Azure