Migrating a Linux VM from one Azure directory to another using ARM templates

Scenario: You want to move an Azure Linux VM from one subscription to another subscription hosted in a different directory. You want to do this using an ARM template.

Assumptions: 

i) You already have a VM running on one of the subscriptions.

ii) For the purpose of this tutorial, I am using a VM running Ubuntu which I will be moving. You could be using any other Linux workload.

iii) The source subscription is called X and the target is called Y.

iv) We will use Powershell to deploy the template but you can also use Bash.

Steps:

      1. We need to download the ARM template of the VM from the source subscription. Let’s call it subscription X.
      2. Login to the Azure portal of subscription X.
      3. Navigate to the VM. Then click on the VM’s Resource Group. On the LHS, click on Automation Script. Then click on Download.  It will download a zip.
        Download template
        Download ARM template
      4. Go to the directory where target subscription Y is located.
      5. Fetch the subscription ID of subscription Y. You can get this by clicking on Overview in any Resource Group and copying subscription Id from there.
      6. Alternatively, type Get-AzureRMSubscription in the Powershell Cloudshell and copy the subscription ID for subscription Y.
      7. In the downloaded zip, open template.json and update all the subscription IDs to the one we copied in the previous step.
      8. If there are any storage accounts in the template.json, update the value to a unique value to avoid errors when running the template. You can search for parameters starting with prefix storageAccounts.
      9. Upload this template to a storage account on subscription Y. Get its blob URL.
      10. Create a resource group in the subscription Y. Let’s call it “RG-DEST-VM”
      11. In the Azure CloudShell, ensure Powershell is selected.
      12. Make sure you have selected the correct subscription

       

    SelectAzureRmSubscription SubscriptionId  <<ID>>

    1. 13. Deploy the template using:

  1. New-AzureRmResourceGroupDeployment -ResourceGroupName “RG-DEST-VM” -TemplateUri  your-blob-url