Create a managed image of a generalized VM in Azure
Applies to: ✔️ Windows VMs
Managed images are helpful in development and test environments where you need a consistent baseline VM. A managed image resource can be created from a generalized virtual machine (VM) that is stored as either a managed disk or an unmanaged disk in a storage account. The image can then be used to create multiple VMs. For information on how managed images are billed, see Managed Disks pricing.
One managed image supports up to 20 simultaneous deployments. Attempting to create more than 20 VMs concurrently, from the same managed image, may result in provisioning timeouts due to the storage performance limitations of a single VHD. To create more than 20 VMs concurrently, use an Azure Compute Gallery (formerly known as Shared Image Gallery) image configured with 1 replica for every 20 concurrent VM deployments.
Prerequisites
You need a generalized VM in order to create an image.
Create a managed image from a VM using the portal
Go to the Azure portal. Search for and select Virtual machines.
Select your VM from the list.
In the Virtual machine page for the VM, on the upper menu, select Capture. The Create an image page appears.
For Share image to Azure compute gallery, select No, capture only a managed image.
For Resource Group, you can either create the image in the same resource group as the VM or select another resource group in your subscription.
For Name, either accept the pre-populated name or type your own name for the image.
If you want to delete the source VM after the image has been created, select Automatically delete this virtual machine after creating the image.
-
- If you want the ability to use the image in any availability zone, select On for Zone resiliency.
Select Create to create the image.
After the image is created, you can find it as an Image resource in the list of resources in the resource group.
Create a managed image of a VM using PowerShell
Creating an image directly from the VM ensures that the image includes all of the disks associated with the VM, including the OS disk and any data disks. This example shows how to create a managed image from a VM that uses managed disks.
Before you begin, make sure that you have the latest version of the Azure PowerShell module. To find the version, run Get-Module -ListAvailable Az
in PowerShell. If you need to upgrade, see Install Azure PowerShell on Windows with PowerShellGet. If you are running PowerShell locally, run Connect-AzAccount
to create a connection with Azure.
Note
If you would like to store your image in zone-redundant storage, you need to create it in a region that supports availability zones and include the -ZoneResilient
parameter in the image configuration (New-AzImageConfig
command).
To create a VM image, follow these steps:
Create some variables.
$vmName = "myVM" $rgName = "myResourceGroup" $location = "EastUS" $imageName = "myImage"
Make sure the VM has been deallocated.
Stop-AzVM -ResourceGroupName $rgName -Name $vmName -Force
Set the status of the virtual machine to Generalized.
Set-AzVm -ResourceGroupName $rgName -Name $vmName -Generalized
Get the virtual machine.
$vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName
Create the image configuration.
$image = New-AzImageConfig -Location $location -SourceVirtualMachineId $vm.Id
Create the image.
New-AzImage -Image $image -ImageName $imageName -ResourceGroupName $rgName
Create an image from a managed disk using PowerShell
If you want to create an image of only the OS disk, specify the managed disk ID as the OS disk:
Create some variables.
$vmName = "myVM" $rgName = "myResourceGroup" $location = "EastUS" $imageName = "myImage"
Get the VM.
$vm = Get-AzVm -Name $vmName -ResourceGroupName $rgName
Get the ID of the managed disk.
$diskID = $vm.StorageProfile.OsDisk.ManagedDisk.Id
Create the image configuration.
$imageConfig = New-AzImageConfig -Location $location $imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -ManagedDiskId $diskID
Create the image.
New-AzImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
Create a managed image from a snapshot using PowerShell
You can create a managed image from a snapshot of a generalized VM by following these steps:
Create some variables.
$rgName = "myResourceGroup" $location = "EastUS" $snapshotName = "mySnapshot" $imageName = "myImage"
Get the snapshot.
$snapshot = Get-AzSnapshot -ResourceGroupName $rgName -SnapshotName $snapshotName
Create the image configuration.
$imageConfig = New-AzImageConfig -Location $location $imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -SnapshotId $snapshot.Id
Create the image.
New-AzImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
Create a managed image from a VM that uses a storage account
To create a managed image from a VM that doesn't use managed disks, you need the URI of the OS VHD in the storage account, in the following format: https://mystorageaccount.blob.core.windows.net/vhdcontainer/vhdfilename.vhd. In this example, the VHD is in mystorageaccount, in a container named vhdcontainer, and the VHD filename is vhdfilename.vhd.
Create some variables.
$vmName = "myVM" $rgName = "myResourceGroup" $location = "EastUS" $imageName = "myImage" $osVhdUri = "https://mystorageaccount.blob.core.windows.net/vhdcontainer/vhdfilename.vhd"
Stop/deallocate the VM.
Stop-AzVM -ResourceGroupName $rgName -Name $vmName -Force
Mark the VM as generalized.
Set-AzVm -ResourceGroupName $rgName -Name $vmName -Generalized
Create the image by using your generalized OS VHD.
$imageConfig = New-AzImageConfig -Location $location $imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsType Windows -OsState Generalized -BlobUri $osVhdUri $image = New-AzImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
Next steps
- Create a VM from a managed image.
- Learn more about using an Azure Compute Gallery (formerly known as Shared Image Gallery)
Feedback
Submit and view feedback for