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 make 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. We can do it at any time, even using the portal, finding the disk we are interested in and clicking on ‘Create snapshot’.
Now the stairs are just beginning. We can create a snapshot by Azure portal only in the subscription in which our disk is located, and only in the region where it is located. This has the consequence that we will not be able to use it so easily in a different location (region) or subscription.
When we create our snapshot, we will not be able to transfer it via the portal.
However, not all is lost, as long as we cannot easily transfer our snapshot using the portal, it is not a problem using PowerShell.
Copying the snapshot
First we need to have a disk snapshot created, this can be done via the portal as shown in the example above. Once we have created a snapshot, we can log in to our Azure subscription using PowerShell:
We can also use Cloud Shell:
Before running the script below, we 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 we do not have a Storage Account created in the target location, we 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 we have move the snapshot to a new location (region), we can easily create a disk using the portal. When creating a disk as a source, select “Storage blob” and select the image we 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.