Categories
Azure Microsoft Powershell

Azure: create VM using a snapshot from a different region

creating VM using a snapshot from a different region

My colleague from a different office was asking me if we could deploy a new VM using an image from a different region. The image was in West Europe, but we needed to deploy the VM in East US.

By Default, you cannot move Disks and Snapshots to a different region. (At least not at the time this post was written) Using Azure Resource Mover, you can move VMs and other resources, but not Disks nor Snapshots. There are two options if you want to deploy VM using a snapshot from a different region.

  • using Azure Resource Mover (Best practice is probably create a new Temporary resource Group and store the VM and related resources in the same resource group) You need to worry about a lot of dependencies and I found it troublesome to use this option.
  • using Azure ASR to replicate the VM to a different region. To do this, you will also need to work on some pre-requisite.

Since I did not want to worry about all these dependencies and pre-requisites, I decided to use PowerShell to copy the snapshot to a blob storage that was in the region that we needed the VM deployed.

There are still some pre-requisites before we can do this. In my case, I already had everything I needed so it was easier for me.

  1. In East US (where we will deploy the VM), we will need a storage Account and a blob created.

That’s it! We only have 1 pre-requisite before we can copy the snapshot to the blob.

#A login window will appear for you to login
Connect-AzAccount 

#define variables
$SourceSnapshotRG = "test_rg_west"
$SourceSnapshotName = "kennytestsnapshot"
$DeststorageAccount = "kennytestrgstorage
$DestBlob = "kennyblob"
$DestStorageAccountRG = "test_rg"
$DestSnapshotName = "NewVM_snapshot"

$StorageAccountKey = (Get-AzStorageAccountKey -Name $DestStorageAccount -ResourceGroupName $StorageAccountRG).value[0]

$snapshot = Get-AzSnapshot -ResourceGroupName $SourceSnapshotRG -SnapshotName $SourceSnapshotName

$DestStorageContext = New-AzStorageContext -StorageAccountName $Deststorageaccount -StorageAccountKey $StorageAccountKey -ErrorAction stop

$snapshotaccess = Grant-AzSnapshotAccess -ResourceGroupName $SourceSnapshotRG -SnapshotName $SourceSnapshotName -DurationInSecond 3600 -Access Read -ErrorAction stop

Start-AzStorageBlobCopy -AbsoluteUri $snapshotaccess.AccessSAS -DestContainer $DestBlob -DestContext $DestStorageContext -DestBlob "$($DestSnapshotname).vhd" -Force -ErrorAction stop


This script will trigger copying the snapshot from the Source to the Destination blob storage by getting an access key that expires in 3600 seconds. Once the copying job is triggered, you will see the NewVM_snapshot.vhd in the Destination storage. It will have a Copy Status of “Pending”. It will change to “Success” after awhile.

Once you have the snapshot in the destination storage account (in the East US region), you can now create a disk from this snapshot.

You can then deploy a VM using this new disk that was just created. This VM will be in the East US region, or whatever region that your storage Account was in.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s