Edit

Use VM managed disks templates

This article describes the differences between managed and unmanaged disks when using Azure Resource Manager templates to provision virtual machines (VMs) in Azure Stack Hub. The examples help you convert existing templates that use unmanaged disks to managed disks.

Note

Unmanaged disks are deprecated. Migrate to managed disks. For details, see Unmanaged disks deprecation.

Unmanaged disks template formatting

To begin, let's look at how to deploy unmanaged disks. When you create unmanaged disks, you need a storage account to hold the VHD files. You can create a new storage account or use an existing one. Create a new storage account resource in the resources block of the template, as follows:

{
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2017-10-01",
    "name": "[variables('storageAccountName')]",
    "location": "[resourceGroup().location]",
    "sku": {
        "name": "Standard_LRS"
    },
    "kind": "Storage"
}

Within the virtual machine object, add a dependency on the storage account so it's created before the virtual machine. Within the storageProfile section, specify the full URI of the VHD location, which references the storage account and is needed for the OS disk and any data disks. The following example creates one OS disk from an image, and one empty data disk with a 1023 GB size:

{
    "type": "Microsoft.Compute/virtualMachines",
    "apiVersion": "2017-12-01",
    "name": "[variables('vmName')]",
    "location": "[resourceGroup().location]",
    "dependsOn": [
    "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
    "[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
    ],
    "properties": {
        "hardwareProfile": {...},
        "osProfile": {...},
        "storageProfile": {
            "imageReference": {
                "publisher": "MicrosoftWindowsServer",
                "offer": "WindowsServer",
                "sku": "[parameters('windowsOSVersion')]",
                "version": "latest"
            },
            "osDisk": {
                "name": "osdisk",
                "vhd": {
                    "uri": "[concat(reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob, 'vhds/osdisk.vhd')]"
                },
                "caching": "ReadWrite",
                "createOption": "FromImage"
            },
            "dataDisks": [
                {
                    "name": "datadisk1",
                    "diskSizeGB": 1023,
                    "lun": 0,
                    "vhd": {
                        "uri": "[concat(reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob, 'vhds/datadisk1.vhd')]"
                    },
                    "createOption": "Empty"
                }
            ]
        },
        "networkProfile": {...},
        "diagnosticsProfile": {...}
    }
}

Managed disks template formatting

With Azure managed disks, the disk becomes a top-level resource and you don't need to create or manage storage accounts. Managed disks also offer many features that unmanaged disks don't. Managed disks were introduced in the 2017-03-30 API version. The following sections walk through the default settings and explain how to further customize your disks.

Default managed disk settings

To create a VM with managed disks, you no longer need to create the storage account resource. In the following template example, some differences exist from the previous unmanaged disk examples:

  • The apiVersion is a version for a "virtualMachines" resource type, which supports managed disks.
  • The osDisk and dataDisks settings no longer refer to a specific URI for the VHD.
  • When you deploy without specifying additional properties, the disk uses a storage type based on the size of the VM. For example, if you're using a VM size that supports premium storage (sizes with "s" in their name such as Standard_DS2_v2), the deployment configures premium disks by default. You can change this default by using the sku setting of the disk to specify a storage type.
  • If you don't specify a name for the disk, it takes the format of <VMName>_OsDisk_1_<randomstring> for the OS disk and <VMName>_disk<#>_<randomstring> for each data disk.
    • If you create a VM from a custom image, the default settings for storage account type and disk name come from the disk properties defined in the custom image resource. You can override these settings by specifying values for them in the template.
  • By default, disk caching is read/write for the OS disk and None for data disks.
  • In the following example, there's still a storage account dependency, though this dependency is only for storage of diagnostics and isn't needed for disk storage:
{
    "type": "Microsoft.Compute/virtualMachines",
    "apiVersion": "2017-12-01",
    "name": "[variables('vmName')]",
    "location": "[resourceGroup().location]",
    "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
        "[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
    ],
    "properties": {
        "hardwareProfile": {...},
        "osProfile": {...},
        "storageProfile": {
            "imageReference": {
                "publisher": "MicrosoftWindowsServer",
                "offer": "WindowsServer",
                "sku": "[parameters('windowsOSVersion')]",
                "version": "latest"
            },
            "osDisk": {
                "createOption": "FromImage"
            },
            "dataDisks": [
                {
                    "diskSizeGB": 1023,
                    "lun": 0,
                    "createOption": "Empty"
                }
            ]
        },
        "networkProfile": {...},
        "diagnosticsProfile": {...}
    }
}

Use a top-level managed disk resource

As an alternative to specifying the disk configuration in the virtual machine object, you can create a top-level disk resource and attach it as part of the virtual machine creation. Be sure to use 2017-03-30 as the disks resource API version. For example, you can create a disk resource as follows to use as a data disk. In this example, vmName is used as part of the disk name:

{
    "type": "Microsoft.Compute/disks",
    "apiVersion": "2017-03-30",
    "name": "[concat(variables('vmName'),'-datadisk1')]",
    "location": "[resourceGroup().location]",
    "sku": {
        "name": "Standard_LRS"
    },
    "properties": {
        "creationData": {
            "createOption": "Empty"
        },
        "diskSizeGB": 1023
    }
}

Within the VM object, reference the disk object to be attached. By specifying the resource ID of the managed disk you created in the managedDisk property, you can attach the disk as the VM is created. Set the apiVersion for the VM resource to 2017-12-01. Add a dependency on the disk resource to ensure it's successfully created before VM creation:

{
    "type": "Microsoft.Compute/virtualMachines",
    "apiVersion": "2017-12-01",
    "name": "[variables('vmName')]",
    "location": "[resourceGroup().location]",
    "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
        "[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]",
        "[resourceId('Microsoft.Compute/disks/', concat(variables('vmName'),'-datadisk1'))]"
    ],
    "properties": {
        "hardwareProfile": {...},
        "osProfile": {...},
        "storageProfile": {
            "imageReference": {
                "publisher": "MicrosoftWindowsServer",
                "offer": "WindowsServer",
                "sku": "[parameters('windowsOSVersion')]",
                "version": "latest"
            },
            "osDisk": {
                "createOption": "FromImage"
            },
            "dataDisks": [
                {
                    "lun": 0,
                    "name": "[concat(variables('vmName'),'-datadisk1')]",
                    "createOption": "attach",
                    "managedDisk": {
                        "id": "[resourceId('Microsoft.Compute/disks/', concat(variables('vmName'),'-datadisk1'))]"
                    }
                }
            ]
        },
        "networkProfile": {...},
        "diagnosticsProfile": {...}
    }
}

Create managed availability sets with VMs using managed disks

To create managed availability sets with VMs using managed disks, add the sku object to the availability set resource and set the name property to Aligned. This property ensures that the disks for each VM are sufficiently isolated from each other to avoid single points of failure. Also note that the apiVersion for the availability set resource is set to 2017-12-01:

{
    "type": "Microsoft.Compute/availabilitySets",
    "apiVersion": "2017-12-01",
    "location": "[resourceGroup().location]",
    "name": "[variables('avSetName')]",
    "properties": {
        "PlatformUpdateDomainCount": 1,
        "PlatformFaultDomainCount": 2
    },
    "sku": {
        "name": "Aligned"
    }
}

Next steps