How to Use Azure Managed Disks

Azure Managed Disks are a powerful and flexible way to manage your virtual machine storage. This guide covers common operations and best practices.

What are Managed Disks?

Managed Disks are a resource manager-enabled storage solution that simplifies disk management for Azure Virtual Machines. They handle the underlying storage account creation, management, and replication.

Creating a Managed Disk

You can create managed disks using the Azure portal, Azure CLI, or PowerShell.

Using Azure CLI

To create a new managed disk, use the az disk create command:


az disk create \
    --resource-group MyResourceGroup \
    --name MyManagedDisk \
    --sku Standard_LRS \
    --size-gb 128 \
    --location eastus
            

This command creates a 128 GB Standard HDD managed disk in the 'eastus' region within the 'MyResourceGroup' resource group.

Using Azure Portal

  1. Navigate to the Azure portal.
  2. Search for "Disks" and select it.
  3. Click "+ Create".
  4. Fill in the required details: Subscription, Resource group, Disk name, Region, and choose a Size and Performance tier.
  5. Click "Review + create".

Attaching a Managed Disk to a VM

Once created, you can attach a managed disk to an Azure Virtual Machine.

Using Azure CLI


az vm disk attach \
    --resource-group MyResourceGroup \
    --vm-name MyVM \
    --name MyManagedDisk
            

This attaches the disk named 'MyManagedDisk' to the VM 'MyVM'.

Using Azure Portal

  1. Navigate to the Virtual Machine you want to attach the disk to.
  2. In the left-hand menu, select "Disks".
  3. Click "+ Add data disk".
  4. Select "Existing disks" and choose your managed disk from the dropdown.
  5. Click "Save".

Detaching a Managed Disk from a VM

To detach a disk, ensure the VM is stopped (deallocated) first.

Using Azure CLI


az vm disk detach \
    --resource-group MyResourceGroup \
    --vm-name MyVM \
    --name MyManagedDisk
            

Using Azure Portal

  1. Navigate to the Virtual Machine.
  2. In the left-hand menu, select "Disks".
  3. Click on the data disk you wish to detach.
  4. Click "Detach disk".
  5. Click "Save" to apply the changes.

Resizing a Managed Disk

You can resize a managed disk without detaching it from the VM in most cases (though OS-level actions might be required).

Using Azure CLI


az disk update \
    --resource-group MyResourceGroup \
    --name MyManagedDisk \
    --size-gb 256
            

This resizes the disk to 256 GB.

Common Scenarios and Best Practices

Tip: Regularly monitor your disk performance using Azure Monitor to ensure you're using the optimal disk type for your workload.
Warning: Always back up critical data before performing major disk operations like resizing or migrating disks.