Step-By-Step: Move a Microsoft Azure VM to a Different Subnet Within a vNet
On May 2nd we help a Microsoft Azure Camp in the MPR (Multi-Purpose Rooms) of our head office in Mississauga Ontario. We had 112 enthusiastic attendees and we covered the following topics:
- Introduction to Windows Azure
- Cloud Storage and Virtual Networks
- Virtual Machines
- Dealing with DEV/TEST scenarios
- Monitoring Public and Hybrid Clouds
- Windows Azure Active Directory
the session was recorded and will be available soon on this blog. but for now, let jump into one question that was asked during the Virtual Networks section.
One attendee asked:
“can you move a machine from one vNet to another without having to re-install it?”
Yes you can... The process to carry out a move of your VM to a different subnet is straight forward:
- Migrate a VM from one subnet to another.
- Update the VM configuration and restart the VM.
Let just step through it.
1- Create a virtual Lets start with a VM created on Subnet-1 of our Public Virtual Network
We already covered in Step-By-Step: Create a Site-to-Site VPN between your network and Azure how to create a Virtual Network with multiple subnets.
2- Create a Virtual Machine in one of the subnets you just created.
In this step we’ll use the “Create from Gallery” option in Azure.
In the third page of the wizard ensure that under REGION/AFFINITY GROUP/VIRTUAL NETWORK section you select the virtual network you just created and select one of the defined subnet.
Complete the machine creation and wait for it to be provisioned and running. Once this machine is created use a PowerShell Script to move it where you need it
3- Use PowerShell to move the machine to a different subnet.
You need to use the following script to move the machine. This particular script imports the Azure Module, adds your Azure Account to the PowerShell session, it asks for the info needed:
- Cloud Service name
- The name of the VM to move
- And the destination subnet to move it to.
# Import Windows Azure PowerShell Module
# Login to Windows Azure subscription
# collect info for the VM move
$ServiceName= read-host -Prompt ("Enter your Cloud Service name")
$VM= read-host -Prompt ("Enter your VM name")
$Subnet= read-host -Prompt ("Enter your destination Subnet name")
# Move the VM
Get-AzureVM -ServiceName $ServiceName -name $VM | Set-AzureSubnet -SubnetNames $Subnet | Update-AzureVM
Prior to moving the VM I validated it’s internal IP address and subnet.
After running the script in this example you can see the Succeeded Operation Status.
Once we received the Succeeded results I refreshed my Azure Portal session and validated the internal IP address and Subnet.
That’s it. Again a great example of how PowerShell brings the power to manage Microsoft Azure in a very straight forward manner.
To continue your learning with Microsoft Azure, visit Microsoft Virtual Academy and complete the Azure for IT Pros Jump Start.