Difan Zhao
Difan Zhao

Reputation: 389

Build a similar VM from an existing VM in Azure

I am new to Azure. I have this existing VM. It was built by my colleague and I think he built it through the Marketplace with the Azure portal.

Now I want to build a new one with the same settings (that have the same performance spec) such as the VM sku, OS disk, and data disk. I don't want to keep any existing data. It will be built in a different RG with a different VNet and subnet. What is the best way to do it?

I tried to "export template" on the current VM but I think the JSON file just specifies the existing disks and NIC to use, instead of creating new ones. Here is what it looks like

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "virtualMachines_myCurrentVM_name": {
        "defaultValue": "myCurrentVM",
        "type": "String"
    },
    "disks_myCurrentVM_OsDisk_1_xxxxxx_externalid": {
        "defaultValue": "/subscriptions/12345678-abcd-abcd-abcd-12345678/resourceGroups/nmtprdarmrgp001/providers/Microsoft.Compute/disks/myCurrentVM_OsDisk_1_xxxxxx",
        "type": "String"
    },
    "disks_myCurrentVM_DataDisk_0_externalid": {
        "defaultValue": "/subscriptions/12345678-abcd-abcd-abcd-12345678/resourceGroups/nmtprdarmrgp001/providers/Microsoft.Compute/disks/myCurrentVM_DataDisk_0",
        "type": "String"
    },
    "networkInterfaces_myCurrentVM290_externalid": {
        "defaultValue": "/subscriptions/12345678-abcd-abcd-abcd-12345678/resourceGroups/nmtprdarmrgp001/providers/Microsoft.Network/networkInterfaces/myCurrentVM290",
        "type": "String"
    }
},
"variables": {},
"resources": [
    {
        "type": "Microsoft.Compute/virtualMachines",
        "apiVersion": "2021-11-01",
        "name": "[parameters('virtualMachines_myCurrentVM_name')]",
        "location": "westus2",
        "tags": {
            "a": "1",
            "b": "2"
        },
        "plan": {
            "name": "f5-bigiq-virtual-edition-byol",
            "product": "f5-big-iq",
            "publisher": "f5-networks"
        },
        "properties": {
            "hardwareProfile": {
                "vmSize": "Standard_D4_v3"
            },
            "storageProfile": {
                "imageReference": {
                    "publisher": "f5-networks",
                    "offer": "f5-big-iq",
                    "sku": "f5-bigiq-virtual-edition-byol",
                    "version": "latest"
                },
                "osDisk": {
                    "osType": "Linux",
                    "name": "[concat(parameters('virtualMachines_myCurrentVM_name'), '_OsDisk_1_xxxxxx')]",
                    "createOption": "FromImage",
                    "caching": "ReadWrite",
                    "managedDisk": {
                        "storageAccountType": "StandardSSD_LRS",
                        "id": "[parameters('disks_myCurrentVM_OsDisk_1_xxxxxx_externalid')]"
                    },
                    "deleteOption": "Detach",
                    "diskSizeGB": 120
                },
                "dataDisks": [
                    {
                        "lun": 0,
                        "name": "[concat(parameters('virtualMachines_myCurrentVM_name'), '_DataDisk_0')]",
                        "createOption": "Attach",
                        "caching": "ReadOnly",
                        "writeAcceleratorEnabled": false,
                        "managedDisk": {
                            "storageAccountType": "StandardSSD_LRS",
                            "id": "[parameters('disks_myCurrentVM_DataDisk_0_externalid')]"
                        },
                        "deleteOption": "Detach",
                        "diskSizeGB": 128,
                        "toBeDetached": false
                    }
                ]
            },
            "osProfile": {
                "computerName": "[parameters('virtualMachines_myCurrentVM_name')]",
                "adminUsername": "azureuser",
                "linuxConfiguration": {
                    "disablePasswordAuthentication": true,
                    "ssh": {
                        "publicKeys": [
                            {
                                "path": "/home/azureuser/.ssh/authorized_keys",
                                "keyData": "ssh-rsa <some key here>"
                            }
                        ]
                    },
                    "provisionVMAgent": true,
                    "patchSettings": {
                        "patchMode": "ImageDefault",
                        "assessmentMode": "ImageDefault"
                    }
                },
                "secrets": [],
                "allowExtensionOperations": true,
                "requireGuestProvisionSignal": true
            },
            "networkProfile": {
                "networkInterfaces": [
                    {
                        "id": "[parameters('networkInterfaces_myCurrentVM290_externalid')]"
                    }
                ]
            },
            "diagnosticsProfile": {
                "bootDiagnostics": {
                    "enabled": true
                }
            }
        }
    }
]

}

Is it the best way to edit and modify the JSON file, or there is another way to do this? Thanks!

Upvotes: 0

Views: 3571

Answers (1)

Madhuraj Vadde
Madhuraj Vadde

Reputation: 1227

Thank you Matan Shabtay. Posting your suggestion as answer to help other community members.

From deployment Section (of the resource group where the VM is currently residing) you would get deployment history. Use that templet to replicate your your VM.

  1. Select the resource group you want to examine. enter image description here

  2. Select the link under Deployments. enter image description here

  3. Select one of the deployments from the deployment history. enter image description here

  4. You can use the view templet option enter image description here

Reference: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deployment-history?tabs=azure-portal

Upvotes: 0

Related Questions