How-to: Migrate a VM between two Azure accounts

Copy your VM to an other Azure account instead of building a new one from scratch
In the Azure IaaS platform, you are able to run virtual machines. These virtual machines are running on an Azure subscription in an Azure account. In some cases, you need to migrate a virtual machine from one Azure account to an other Azure account. This how-to will explain the steps that are needed to perform a virtual machine migration between two Azure accounts. Please note that this how-to describes the steps that are needed to perform a migration between two Azure account. If you only need to migrate a VM between two subscriptions in the same account, there are better solutions.

High level steps

Azure doesn’t support an out-of-the-box Virtual Machine migration. This how-to will describe a scenario that is close to a migration. On a high level, the following steps are required:
  • Turn off the VM
  • Copy the VHD of the source VM to an storage account in the target Azure account.
  • Create a new VM based on the copied VHD in the target Azure account
Since this a close to migration scenario there are a couple of things to keep in mind:
  • The IP address of the VM will change.
  • The rules that are defined in the network security group are not migrated
  • There will be downtime during the migration

Copy VHD to the other Azure account

The first step in this how-to is turning off the Azure Virtual Machine. After turning off the VM, you are able to start a blob storage copy. The following PowerShell commands are used to do the copy:
### The URI of the VHD we would like to migrate
$sourceVhdUri = ""
### Source storage account definition
$sourceStorageAccountName = "mysourcestorageaccount"
$sourceStorageKey = "verysecretkey"
### Destination storage account definition
$destStorageAccountName = "mytargetstorageaccount"
$destStorageKey = "thismustbeasecret"
### The source context
$sourceContext = New-AzureStorageContext  –StorageAccountName $sourceStorageAccountName `
                                        -StorageAccountKey $sourceStorageKey  
### The target context
$destContext = New-AzureStorageContext  –StorageAccountName $destStorageAccountName `
                                        -StorageAccountKey $destStorageKey  
### Name for the destination storage container
$containerName = "migratedvhds"
### Create a new container in the destination storage, the VHD will be placed in this container
New-AzureStorageContainer -Name $containerName -Context $destContext 
### Start the copy action
$blob = Start-AzureStorageBlobCopy -srcUri $sourceVhdUri `
                                    -SrcContext $sourceContext `
                                    -DestContainer $containerName `
                                    -DestBlob "myvhd.vhd" 
After running the above commands, an ansynchronous copy has been started. You can view the status of the copy by running:
### Output the status of the copy
$blob | Get-AzureStorageBlobCopyState

Create a virtual machine based on the copied VHD

After the copy has been completed it is time to build a new virtual machine based on that VHD. The only way to build a VM based on an existing VHD is via PowerShell. The following commands are needed to create a new virtual machine based on an existing VHD (the copied one).
#Create Resource Group
$resourceGroupName = "MyResourceGroup"
$resourceGroup = New-AzureRmResourceGroup -Name $resourceGroupName -Location "westeurope" 

# Select Vritual Network
$virtualNetworkName = "MyVirtualNetwork"
$locationName = "westeurope"

$susbnet = New-AzureRmVirtualNetworkSubnetConfig -Name FrontEnd -AddressPrefix 

$virtualNetwork = New-AzureRmVirtualNetwork -Name $virtualNetworkName `
                                            -Subnet $susbnet `
                                            -Location $locationName `
                                            -ResourceGroupName $resourceGroupName `

# Create IP-Adres
$publicIp = New-AzureRmPublicIpAddress -Name "MyPublicIpAddress" `
                                       -ResourceGroupName $resourceGroupName `
                                       -Location $locationName -AllocationMethod Dynamic

# Create Network Interface
$networkInterface = New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName `
                                                -Name "MyNetworkInterface"  `
                                                -Location $locationName `
                                                -SubnetId $virtualNetwork.Subnets[0].Id `
                                                -PublicIpAddressId $publicIp.Id `

# VM Config
$vhdUri = ""
$vmConfig = New-AzureRmVMConfig -VMName "MyVirtualMachine" -VMSize "Standard_A1"
$vmConfig = Set-AzureRmVMOSDisk -VM $vmConfig -Name "MyVMDisk" -VhdUri $vhdUri `
                                -CreateOption Attach -Windows
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $networkInterface.Id

#Create VM
$vm = New-AzureRmVM -VM $vmConfig -Location $locationName -ResourceGroupName $resourceGroupName

Migration Complete

After completing the above steps, the migration has done. Your can now start your virtual machine. Please note that the IP address of the virtual machine is changed. You may need to change some DNS records or IP address configured in applications that are depending on this virtual machine.
Share This