azureazure-virtual-machineazure-managed-disk

Creating a managed disk from snapshot in different region (Azure)


I seem to have a problem creating a managed disk from a snapshot.

It appears that I can only create a disk in one region which is West US.

Here is the PowerShell script I use:

Get-AzureRmSubscription –SubscriptionName 'MySubscription' | Select-AzureRmSubscription

$resourceGroupName = 'MyResourceGroup';
$diskName = 'MyNewDisk';
$location = 'West US';

$snapshotName = 'MySnapshot';
$snapshot = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName;

$diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $location -SourceResourceId $snapshot.Id -CreateOption Copy;
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName;

If I change the variable $region value to 'East US' or any other region, I get en error in PowerShell (resource not found).

The snapshot itself is in West US but I want to create a disk in East US. What am I doing wrong?


Solution

  • The snapshot itself is in West US but I want to create a disk in East US. What am I doing wrong?

    We can't create a managed disk from snapshot to another location.

    If you want to create a managed disk from snapshot to another location, we should export/Copy managed snapshots as VHD to a storage account in different region with PowerShell.

    Here the sample script:

    #Provide the subscription Id of the subscription where snapshot is created
    $subscriptionId = "yourSubscriptionId"
    
    #Provide the name of your resource group where snapshot is created
    $resourceGroupName ="yourResourceGroupName"
    
    #Provide the snapshot name 
    $snapshotName = "yourSnapshotName"
    
    #Provide Shared Access Signature (SAS) expiry duration in seconds e.g. 3600.
    #Know more about SAS here: https://learn.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1
    $sasExpiryDuration = "3600"
    
    #Provide storage account name where you want to copy the snapshot. 
    $storageAccountName = "yourstorageaccountName"
    
    #Name of the storage container where the downloaded snapshot will be stored
    $storageContainerName = "yourstoragecontainername"
    
    #Provide the key of the storage account where you want to copy snapshot. 
    $storageAccountKey = 'yourStorageAccountKey'
    
    #Provide the name of the VHD file to which snapshot will be copied.
    $destinationVHDFileName = "yourvhdfilename"
    
    
    # Set the context to the subscription Id where Snapshot is created
    Select-AzureRmSubscription -SubscriptionId $SubscriptionId
    
    #Generate the SAS for the snapshot 
    $sas = Grant-AzureRmSnapshotAccess -ResourceGroupName $ResourceGroupName -SnapshotName $SnapshotName  -DurationInSecond $sasExpiryDuration -Access Read 
    
    #Create the context for the storage account which will be used to copy snapshot to the storage account 
    $destinationContext = New-AzureStorageContext –StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey  
    
    #Copy the snapshot to the storage account 
    Start-AzureStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $storageContainerName -DestContext $destinationContext -DestBlob $destinationVHDFileName
    
    #Monitor status
    Get-AzureStorageBlobCopyState -Context $destinationContext -Blob $destinationVHDFileName -Container $storageContainerName
    

    More information about it, please refer to this link.