This article covers the older managed image technology. For the most current technology, customers are encouraged to use Azure Compute Gallery. All new features, like ARM64, Trusted Launch, and Confidential VM are only supported through Azure Compute Gallery. If you have an existing managed image, you can use it as a source and create an Azure Compute Gallery image. For more information, see Create an image definition and image version.
Important
A legacy managed image cannot be created from a virtual machine deployed using Trusted Launch.
Once you mark a VM as generalized in Azure, you cannot restart the VM.
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.
For information on how managed images are billed, see
Managed disk pricing.
CLI: Create a legacy managed image of a VM
Create a legacy managed image of the VM with
az image create. Creating a legacy managed 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.
The legacy managed image is created in the same resource group as your source VM. You can create VMs in any resource group within your subscription from this image. From a management perspective, you may wish to create a specific resource group for your VM resources and images.
The following example
creates a legacy managed image named myImage in the resource group named
myResourceGroup using the VM resource named myVM.
az image create \
--resource-group myResourceGroup \
--name myImage --source myVM
If you are capturing an image of a generation 2 VM, also use the --hyper-v-generation V2 parameter. for more information, see Generation 2 VMs.
If you would like to store your image in zone-resilient storage, you need to create it in a region that supports availability zones and include the --zone-resilient true parameter.
This command returns JSON that describes the VM image. Save this output for later reference.
PowerShell: Create a legacy managed image of a VM
This example shows how to create a legacy managed image from a VM. Creating a legacy managed 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.
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.
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 legacy managed image of a VM, follow these steps:
Create some variables.
$vmName = "myVM"
$rgName = "myResourceGroup"
$location = "Central US"
$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.
$imageConfig = New-AzImageConfig -Location $location -SourceVirtualMachineId $vm.Id
Create the image.
New-AzImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
CLI: Create a VM in another resource group from a legacy managed image
You can create VMs from a legacy managed image in any resource group within your
subscription. To create a VM in a different resource group than the legacy managed image,
specify the full resource ID. Use
az image list to view a list of images.
The output is similar to the following example.
"id":
"/subscriptions/guid/resourceGroups/MYRESOURCEGROUP/providers/Microsoft.Compute/images/myImage",
"location": "westus", "name": "myImage",
The following example uses az vm create to
create a VM in a resource group other than the source legacy managed image, by
specifying the image resource ID.
az vm create \
--resource-group myOtherResourceGroup \
--name myOtherVMDeployed \
--image "/subscriptions/guid/resourceGroups/MYRESOURCEGROUP/providers/Microsoft.Compute/images/myImage" \
--admin-username azureuser \
--ssh-key-value ~/.ssh/id_rsa.pub
PowerShell: Create a VM from a legacy managed image
You can use PowerShell to create a VM from a legacy managed image by using the
simplified parameter set for the
New-AzVm cmdlet. The legacy managed image needs
to be in the same resource group where you'll create the VM.
The simplified parameter set for
New-AzVm only requires that you
provide a name, resource group, and image name to create a VM from an
image. New-AzVm will use the value of the -Name parameter as the name
of all of the resources that it creates automatically. In this example, we
provide more detailed names for each of the resources but let the cmdlet
create them automatically. You can also create resources beforehand, such
as the virtual network, and pass the resource name into the cmdlet.
New-AzVm will use the existing resources if it can find them by their
name.
The following example creates a VM named myVMFromImage, in the
myResourceGroup resource group, from the image named myImage.
New-AzVm `
-ResourceGroupName "myResourceGroup" `
-Name "myVMfromImage" `
-ImageName "myImage" `
-Location "East US" `
-VirtualNetworkName "myImageVnet" `
-SubnetName "myImageSubnet" `
-SecurityGroupName "myImageNSG" `
-PublicIpAddressName "myImagePIP"