'creating vm from snapshot when the snapshot is in another resource group

I have created vm from snapshot using azure cli command Below is the script for the creation of vm from snapshot

az disk create -g $RD_OPTION_RESOURCEGROUP -n $RD_OPTION_DISKNAME --source $RD_OPTION_SNAPSHOTNAME
az vm create -g $RD_OPTION_RESOURCEGROUP -n $RD_OPTION_VMNAME --attach-os-disk $RD_OPTION_DISKNAME --os-type windows

This code will create the vm from snapshot in which snapshot is in one resource group say abc then the newly creating vm should also in same resource group abc

now i need to find the way like i want to create a vm from snapshot like the snapshot and newly creating vm will be in different resource groups... like if snapshot is in abc resource group then the newly creating vm have to be allocated in the other resource group say xyz

Please help me out in this



Solution 1:[1]

To create a VM from Snapshot which is in different resource group, copy the snapshot using below CLI script:

  • Define a variable as sourceSubscriptionId where you have to provide the subscription ID in which your snapshot resides.
sourceSubscriptionId="<subscriptionId>"
  • Define a variable as sourceResourceGroupName where you have to provide the resource group name in which your snapshot resides.
sourceResourceGroupName=yourSourceResourceGroupName
  • Define a variable where you have to provide snapshot name.
snapshotName=mySnapshotName
  • Set the context to the subscription Id where snapshot exists.
az account set --subscription $sourceSubscriptionId
  • Use the below cmdlet to get the snapshot ID.
snapshotId=$(az snapshot show --name $snapshotName --resource-group $sourceResourceGroupName --query [id] -o tsv)
  • Define a variable targetResourceGroupName where snapshot will be copied to:
targetResourceGroupName=mytargetResourceGroupName
  • To copy the snapshot to different resource group try using the below cmdlet:
az snapshot create --resource-group $targetResourceGroupName --name $snapshotName --source $snapshotId --sku Standard_LRS

Reference : azure-cli-samples/copy-snapshot-to-same-or-different-subscription.sh at master · Azure-Samples/azure-cli-samples · GitHub.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RukminiMr-MT