Move a Marketplace Azure Virtual Machine to another subscription
Applies to: ✔️ Linux VMs ✔️ Windows VMs ✔️ Flexible scale sets
To move a Marketplace virtual machine to a different subscription, you must move the OS disk to that subscription and then recreate the virtual machine.
You don't need this procedure to move a data disk to a new subscription. Instead, create a new virtual machine in the new subscription from the Marketplace, then move and attach the data disk.
This script demonstrates three operations:
- Create a snapshot of an OS disk.
- Move the snapshot to a different subscription.
- Create a virtual machine based on that snapshot.
Prerequisites
Use the Bash environment in Azure Cloud Shell. For more information, see Quickstart for Bash in Azure Cloud Shell.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
Sample script
To move a Marketplace virtual machine to a different subscription, you must create a new virtual machine for the same Marketplace offer from the moved OS disk.
Note
If the virtual machine plan is no longer available in the Marketplace, you can't use this procedure.
#!/bin/bash
# Set variable values before proceeding.
# Variables
sourceResourceGroup= Resource group for the current virtual machine
sourceSubscription= Subscription for the current virtual machine
vmName= Name of the current virtual machine
destinationResourceGroup= Resource group for the new virtual machine, create if necessary
destinationSubscription= Subscription for the new virtual machine
# Set your current subscription for the source virtual machine
az account set --subscription $sourceSubscription
# Load variables about your virtual machine
# osType = windows or linux
osType=$(az vm get-instance-view --resource-group $sourceResourceGroup \
--name $vmName --subscription $sourceSubscription \
--query 'storageProfile.osDisk.osType' --output tsv)
# offer = Your offer in Marketplace
offer=$(az vm get-instance-view --resource-group $sourceResourceGroup \
--name $vmName --query 'storageProfile.imageReference.offer' --output tsv)
# plan = Your plan in Marketplace
plan=$(az vm get-instance-view --resource-group $sourceResourceGroup \
--name $vmName --query 'plan' --output tsv)
# publisher = Your publisher in Marketplace
publisher=$(az vm get-instance-view --resource-group $sourceResourceGroup \
--name $vmName --query 'storageProfile.imageReference.publisher' --output tsv)
# Get information to create new virtual machine
planName=$(az vm get-instance-view --resource-group $sourceResourceGroup \
--subscription $sourceSubscription --query 'plan.name' --name $vmName)
planProduct=$(az vm get-instance-view --resource-group $sourceResourceGroup \
--subscription $sourceSubscription --query 'plan.product' --name $vmName)
planPublisher=$(az vm get-instance-view --resource-group $sourceResourceGroup \
--subscription $sourceSubscription --query 'plan.publisher' --name $vmName)
# Get the name of the OS disk
osDiskName=$(az vm show --resource-group $sourceResourceGroup --name $vmName \
--query 'storageProfile.osDisk.name' --output tsv)
# Verify the terms for your market virtual machine
az vm image terms show --offer $offer --plan '$plan' --publisher $publisher \
--subscription $sourceSubscription
# Deallocate the virtual machine
az vm deallocate --resource-group $sourceResourceGroup --name $vmName
# Create a snapshot of the OS disk
az snapshot create --resource-group $sourceResourceGroup --name MigrationSnapshot \
--source "/subscriptions/$sourceSubscription/resourceGroups/$sourceResourceGroup/providers/Microsoft.Compute/disks/$osDiskName"
# Move the snapshot to your destination resource group
az resource move --destination-group $destinationResourceGroup \
--destination-subscription-id $destinationSubscription \
--ids "/subscriptions/$sourceSubscription/resourceGroups/$sourceResourceGroup/providers/Microsoft.Compute/snapshots/MigrationSnapshot"
# Set your subscription to the destination value
az account set --subscription $destinationSubscription
# Accept the terms from the Marketplace
az vm image terms accept --offer $offer --plan '$plan' --publisher $publisher \
--subscription $destinationSubscription
# Create disk from the snapshot
az disk create --resource-group $destinationResourceGroup --name DestinationDisk \
--source "/subscriptions/$destinationSubscription/resourceGroups/$destinationResourceGroup/providers/Microsoft.Compute/snapshots/MigrationSnapshot" \
--os-type $osType
# Create virtual machine from disk
az vm create --resource-group $destinationResourceGroup --name $vmName \
--plan-name $planName --plan-product $planProduct --plan-publisher $planPublisher \
--attach-os-disk "/subscriptions/$destinationSubscription/resourceGroups/$destinationResourceGroup/providers/Microsoft.Compute/disks/DestinationDisk" \
--os-type $osType
Clean up resources
After the sample has been run, use the following commands to remove the resource groups and all associated resources:
az group delete --name $sourceResourceGroup --subscription $sourceSubscription
az group delete --name $destinationResourceGroup --subscription $destinationSubscription
Azure CLI references used in this article
- az account set
- az disk create
- az group delete
- az resource move
- az snapshot create
- az vm create
- az vm deallocate
- az vm delete
- az vm get-instance-view
- az vm image terms accept
- az vm image terms show
- az vm show