How to Assign a Private Static IP to an Azure VM
Having a private Static IP is required for some services to run properly in a Cloud environment. One of these services is DNS where you cannot have a reliable DNS resolution system as long as your DNS servers do not have static IPs and may change their IPs whenever they get rebooted. This Wiki article shares how you can assign a static IP to an Azure VM to overcome the previous limitation of Azure.
Before proceeding, you need to install the latest available version of Azure Powershell.
How to install and configure Azure PowerShell: http://www.windowsazure.com/en-us/documentation/articles/install-configure-powershell/
Script to use to assign a private static IP to a VM
The following Powershell script can be used to assign a Static IP to an Azure VM. You need first to update the values of the following variables:
- $IPaddress: You need to specify the static IP address to assign to your VM
- $VNETName: You need to specify the Name of the Virtual Network that the Static IP address belongs to
- $VMName: The name of the VM to update
- $ServiceName: The name of the cloud service to which the VM belongs
##############Variables#################
$IPaddress = "192.168.0.13"
$VNETName = "TestNetwork"
$VMName = "CONTOSOVMTEST1"
$ServiceName = "CONTOSOVMTEST1"
########################################
##############Main######################
if (Test-AzureStaticVNetIP -VNETName $VNETName -IPAddress $IPaddress)
{
$VM = Get-AzureVM -ServiceName $ServiceName -Name $VMName
Set-AzureStaticVNetIP -VM $VM -IPAddress $IPaddress | Update-AzureVM
$VMStaticIPCheck = Get-AzureStaticVNetIP -VM $VM
if ($VMStaticIPCheck -eq $null)
{
"No Static IP address was assigned to " + $VMName + " VM"
}
else
{
If ($VMStaticIPCheck.IPAddress -eq $IPaddress)
{
$displaymessage = "The IP address " + $IPaddress + " was successfully assigned to " + $VMName
write-host $displaymessage -foreground "Green"
}
else
{
$displaymessage = "The IP address " + $IPaddress + " was not assigned to " + $VMName + ". The VM is using " + $VMStaticIPCheck.IPAddress + " as IP address"
write-host $displaymessage -foreground "Red"
}
}
}
else
{
$displaymessage = $IPaddress + " IP address is not available."
write-host $displaymessage -foreground "Red"
}
How is the Script Working
The script will check if the static IP to assign using Test-AzureStaticVNetIP cmdlet. If the returned value is $true then the IP is available. If the IP is available, the script will set a static IP to the VM using Set-AzureStaticVNetIP and Update-AzureVM cmdlets.
Testing and Supportability
The script was tested on Azure VMs and we have seen that, when the updates are done, the VM is rebooted and has the new IP assigned.
A dynamic IP is still configured in Internet Protocol Version 4 (TCP/IPv4) Properties as the static IP is managed on the Azure level and not on the VM level.
As for the ability to support, “Static IP addresses are supported on Azure virtual machines, but they must be configured by using Azure PowerShell.”.
Reference: http://msdn.microsoft.com/en-us/library/windowsazure/jj156090.aspx
Remark: The Powershell cmdlets described in this Wiki article might behave differently depending on the Region where the VM resides.