How to Assign a Private Static IP to an Azure VM

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 PowerShellhttp://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 a 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 supportability, “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 of the Region where the VM resides.
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Added tags

  • how does this work when a machine is moved between hosts / environments in Azure and how does it work with virtual networking in azure? Azure already assigns a permanent address to a VM when you are using virtual networking - and that ports across through the entire Azure environment.

  • how does this work when a machine is moved between hosts / environments in Azure and how does it work with virtual networking in azure? My understanding was Azure already assigns a permanent address to a VM when you are using virtual networking - and that ports across through the entire Azure environment.

  • how does this work when a machine is moved between hosts / environments in Azure and how does it work with virtual networking in azure? My understanding was Azure already assigns a permanent address to a VM when you are using virtual networking - and that ports across through the entire Azure environment.

Page 1 of 1 (4 items)