Moving Azure VM’s with managed disks to new subscription

One of the things that I’ve come up against recently is a limitation with managed disks. Today it was specifically when it comes to moving virtual machines that use them into a new subscription. In short, it’s not doable – the currently documented limitations of managed disks state that this isn’t possible. However there is a workaround – but it does take a little bit of legwork to do. I’ll run you through the process I followed here though.

Step 1: Create Blob images of the managed disks

The first thing you need to do is to get copies of your managed disks out into a standard blob storage account so that they can be migrated. There are a few PowerShell cmdlets that we can use for this purpose. Below is a short script that shows an example of how this can be done. It assumes that you have already created a storage account in your resource group that you can move to the new subscription, this will store the blobs for each disk you wish to move.

$diskName = "Name of your managed disk resource"
$token = Grant-AzureRmDiskAccess -ResourceGroupName "Your RG name" -DiskName $diskName -Access Read -DurationInSecond 36000
$storageAccount = Get-AzureRmStorageAccount -ResourceGroupName "Your RG name" -Name "YourStorageAccountName"
Start-AzureStorageBlobCopy -AbsoluteUri $token.AccessSAS -DestContainer 'vhds' -DestContext $storageAccount.Context -DestBlob "$diskName.vhd"

In this script, you’ll want to make sure you update the name of your disk, the resource group names and the storage account name. Basically what you are doing is created a temporary SAS token that can read the disk and use that to copy it into blob storage. We then kick off a blob copy in the background to let it complete. Once the copy has completed we can move on to the next step.

Step 2: Move resources to the new subscription

Next, you will move your resources to the new subscription. You can do this through the portal with the “Move” option on the resource group – just be sure to not move any virtual machine resources that use managed disks as this will fail. At this point, you will just want to move over the storage account you just created that has your storage blob in it. If you like you can delete the virtual machines so that things like network adapters, public IP addresses, virtual networks, etc. can be moved as well. Just be sure that any other config on the VM can be restored on the other side before you go deleting things!

Step 3: Set up the managed disks in the destination subscription

The storage blobs are now in the destination subscription, so we can now use them to re-establish some managed disks. Here is how to do this with Azure PowerShell.

$disk1Url = "URL to your disk blob"
$diskConfig = New-AzureRmDiskConfig -AccountType StandardLRS -Location "Your location" -CreateOption Import -SourceUri $disk1Url
$osDisk = New-AzureRmDisk -DiskName "DiskName" -ResourceGroupName "Your RG Name" -Disk $diskConfig

There are two steps here – create the disk config, then create a new disk. We use the URL of the migrated disk to do this. Make sure you update your resource group name, location and disk name values.

Step 4: Create new virtual machines

Your resources are now in the destination subscription, so its time to recreate the virtual machines. If you brought across network adapters and IP addresses, this will be a very short process as you can use the existing resources. For help creating the virtual machines from scratch check out this tutorial. But if you migrated the network adapters and other resources, you can use a bit of code like mine below.

$nic = Get-AzureRmNetworkInterface -Name "Your NIC name" -ResourceGroupName "Your RG name"
$vmConfig = New-AzureRmVMConfig -VMName "Your Server Name" -VMSize "Standard_A1"
$vmconfig = Add-AzureRmVMNetworkInterface -VM $vmconfig -Id $nic.Id
$vmConfig = Set-AzureRmVMOSDisk -VM $vmConfig -ManagedDiskId $osDisk.Id -StorageAccountType StandardLRS -DiskSizeInGB 128 -CreateOption Attach -Windows
$vmConfig = Add-AzureRmVMDataDisk -VM $vmConfig -ManagedDiskId $secondDisk.Id -Lun 0 -CreateOption Attach
New-AzureRmVM -ResourceGroupName "Your RG name" -Location "Your location" -VM $vmConfig

Before you run the script be sure to review it and update all of your variables and values to make them correct for your subscription. In this case, I also had 2 disks to attach to my machine, so if you have only one remove the line with “Add-AzureRmVMDataDisk”.

Step 5: Clean up storage accounts

The VM is now up running again with a new managed disk, so you can now clean things up. To do this you only need to delete the storage account that held the blob images that you used to create the disks in the destination subscription. That’s it though, you’re done! Not as easy as point and click through the portal, but not too hard to script.

One Reply to “Moving Azure VM’s with managed disks to new subscription”

Leave a Reply