In the last post, I showed how to replace disks in Azure Virtual Machines using the PowerShell script. You only need a disk image and you can quickly perform the replacement. Today’s post will complement the previous one and will show you how to create a disk image and move it to any location (region). We will take a snapshot of the VM (virtual machine) disk and move it to another region.
Snapshot, that is, simply speaking, a copy of the disk made at a given moment. You can do it at any time, even using the portal, finding the disk and clicking on ‘Create snapshot‘.
Now the stairs are just beginning. You can create a snapshot by Azure portal only in the subscription in which your disk is located, and only in the region where it is located. This has the consequence that you will not be able to use it so easily in a different location (region) or subscription.
When you create snapshot, you will not be able to transfer it via the portal to other region.
However, not all is lost. As long as you cannot easily transfer your snapshot using the portal, it is not a problem using PowerShell.
Copying the snapshot
First you need to have a disk snapshot created, this can be done via the portal as shown in the example above. When you have created a snapshot, you can log in to your Azure subscription from your computer using PowerShell:
You can also use Cloud Shell:
Before running the script below, you need to correct the variable names in it.
Enter the name of the snapshot you want to move and its resource group.
The destination will be Storage Account. We give its name, the name of the BLOB to which the disk image will go, and the name of the resource group in which it is located. Finally, the name of the created disk image.
If you do not have a Storage Account created in the target location, you need to create it.
# SOURCE $SnapshotResourceGroup = "ResourceGroupNAME1" $SnapshotName = "SnapshotName" # DESTINATION $StorageAccount = "StorageAccountNAME" $StorageAccountBlob = "StorageAccountBlobNAME" $storageaccountResourceGroup = "ResourceGroupNAME2" $vhdname = "disk_image_name" #SA_KEY $StorageAccountKey = (Get-AzStorageAccountKey -Name $StorageAccount -ResourceGroupName $StorageAccountResourceGroup).value $snapshot = Get-AzSnapshot -ResourceGroupName $SnapshotResourceGroup -SnapshotName $SnapshotName #GRANTING ACCESS $snapshotaccess = Grant-AzSnapshotAccess -ResourceGroupName $SnapshotResourceGroup -SnapshotName $SnapshotName -DurationInSecond 3600 -Access Read -ErrorAction stop $DestStorageContext = New-AzStorageContext –StorageAccountName $storageaccount -StorageAccountKey $StorageAccountKey -ErrorAction stop Write-Output "START COPY" Start-AzStorageBlobCopy -AbsoluteUri $snapshotaccess.AccessSAS -DestContainer $StorageAccountBlob -DestContext $DestStorageContext -DestBlob "$($vhdname).vhd" -Force -ErrorAction stop Write-Output "END COPY"
Create a disk
When you have move the snapshot to a new location (region), you can easily create a disk using the portal. When you creating a disk as a source, select “Storage blob” and select the image you have just created. Please note that the created disk cannot be smaller than a snapshot.
You can learn how to replace the disk itself in a virtual machine from the previous article: https://lepczynski.it/en/azure_en/swap-the-os-disk-of-an-azure-vm/
Of course, you can automate everything. I gave a simple example that you can move a snapshot to another region. You might as well create a more complicated script that, after specifying the source disk name, resource group, and target location:
- create a Storage Account in the target location,
- create a snapshot,
- move the snapshot to the target location,
- create a snapshot disk at the target location,
- delete unnecessary snapshot,
- remove unnecessary Storage Account.
It all depends mainly on our ingenuity, remember that. If you made it to the end and would like more advanced stuff, leave comments and I will try to do a more advanced post.
Another way to move the disk
By the way, there are other ways that you can move the drive to another region without even using PowerShell. Assuming that we have a VM in region A that we want to copy to region B, we can, for example … Create a new resource group in region A. Copy the VM to it and move the entire resource group (containing a copy of our VM) to a new location, region B. This operation can be performed entirely via the portal, but for me it is more time consuming and I do not use it;)
Moving Azure VMs to another Azure region: link with documentation.