Assign Static IP To Azure Virtual Machines Within A Virtual Network

When we want multiple VM's that should be part of a network, we will be creating the VM's within a Virtual Network. We can create a Virtual Network initially and when the VM is created, we can specify the Virtual Network in the field to add the VM to a specific Virtual Network.



What is the need for static IP?

Once the VM is created, it will be assigned a dynamic IP within the Virtual Network. However, at a later point, when the VM is shut down and deallocated or when a new VM is created, the IP of the already existing VM may be displaced. In order to overcome this, once the VM is created, we will be assigning a static IP to the VM, so that the IP within the Virtual Network is reserved for the VM throughout its lifetime.

How to assign static IP to the VM?

We can assign static IP to the VM, using Azure PowerShell. If Azure PowerShell is not installed, you can check out this article to get an overview. Once Microsoft Azure PowerShell is installed, open it as administrator. Add the command, given below, to the command line to connect to Azure Account.

Add-AzureAccount



This will open up a pop up, where we can add Azure account credentials. Click Sign In.



If it successfully authenticated, the user needs to show the user Id and subscription Id, as shown above. Run the command, given below, to get all the subscription details of the logged in user. If there are multiple subscriptions, we will have to select the specific subscription within which we had created the Virtual Network and the VM.

Get-AzureSubscription

This will get all the subscriptions of the user, as shown below. Use the ‘Select-AzureSubscription’ to pick a specific subscription against which we will run the PowerShell cmdlet.

Select-AzureSubscription –SubscriptionId “Id of the subscription”

You can also pick Azure subscription by specifying the subscription name. However, if there are two subscriptions with the same name, it won’t work out. We will have to use subscription Id in such a case.



Thus, we have used PowerShell to connect to user’s Azure account and to the specific subscription within which we have created the Virtual Network. Now, let’s check the available IP addresses within the Virtual Network, so that VM's can be assigned a static IP. When the VM is created, an IP will be assigned to it. However, if we don’t assign a static IP, using PowerShell, the next VM is creates and the IP of the previously created VM may change. In order to avoid it, once the VM is created, we will assign an available static IP to the VM, so that it will be reserved for the VM throughout its lifetime.

Run the script, given below, to get the available list of IP's within the Virtual Network.

Test-AzureStaticVnetIP –VnetName “Name of the Virtual Network” –IPAddress “Test IP”

This will check if the Test IP is available to be assigned to a VM. If it is available, it will return the “IsAvailable” parameter as true. If it is not available, it returns the available IPs as comma separated value in the parameter “AvailableAddresses”.



Now, we know the available IP addresses within the Virtual Network. Let’s assign one of them to the previously created VM, using the cmdlet:

Get-AzureVM -ServiceName "VM01-AzureAD" -Name "VM01-AzureAD"|Set-AzureStaticVNetIP -IPAddress 10.0.0.5 | Update-AzureVM

  • ServiceName is the name of the Cloud hosted Service name, assigned during VM creation.
  • Name is the name of the VM.


Thus, PowerShell script has successfully updated the VM’s static IP address. Let’s head over to the VM’s dashboard to see, if it has been reflected.



As you can see above, it has been updated in the dashboard as well.

Summary - Thus, we saw how to assign a static IP to the Virtual Machines in a Virtual Network in Azure.