M365Con 2027 – free online conference · 11–22 Jan 2027 · 120 sessions m365con.net

Blog · Mirko Peters

Azure Image Builder for AVD: building images as code

Partner · Neutral partner: Nerdio

By ·

Azure Image Builder turns a JSON template into a finished, generalized VM image and distributes it to Azure Compute Gallery or a managed image. This article explains how the template works, how AVD custom image templates relate to it, and which prerequisites, limits and costs apply.

Azure VM Image Builder, often called Azure Image Builder or AIB, builds a VM image from a configuration you submit as a template. The service creates a temporary build VM, runs your customizations, generalizes the image and distributes the result to an Azure Compute Gallery, a managed image or a VHD. For Azure Virtual Desktop it offers a repeatable template as an alternative to hand-built golden images. This article explains the template, how to start a build and find its log, the AVD-specific route and the limits.

Key points

  • Azure VM Image Builder is a managed service built on HashiCorp Packer. You submit a template naming a source, customizations and distribution targets, and the service builds the image.
  • AVD custom image templates run on Azure Image Builder, GA in Azure public cloud since September 2023. The portal adds built-in scripts; a direct template gives full JSON control.
  • The template has three main sections: a source (platform image, managed image or gallery image version), a list of customizers that run in order, and one or more distribution targets.
  • Build timeout defaults to 240 minutes, with a range of 6 to 960 minutes. For Windows, Learn advises not going below 60 minutes and using at least a Standard_D2_v2 build VM.
  • Image Builder does not support Azure Hybrid Benefit for Windows Server, and the image template resource is immutable and cannot be moved.

Microsoft's What's new page and the Azure Virtual Desktop blog announced custom image templates as generally available in September 2023. The blog post names the Azure public cloud and says Azure Government and China entered public preview at that time; the custom image templates pages carry no status label, so check them for your cloud. The overview labels the service's Azure DevOps task as preview. Lifecycle practice and gallery replication are covered in "The golden image lifecycle: build, test, release, retire" and "Azure Compute Gallery for AVD images: versions and replication".

What the service does

Learn's overview describes a fully managed Azure service built on HashiCorp Packer. It works with Azure Marketplace base images and existing custom images, for Windows and Linux, and for generation 1 and generation 2 Hyper-V images. The distributed image always has the generation of the source image. It hides the need to generalize with Sysprep, though advanced users can override that.

You can pass a configuration with Azure PowerShell, the Azure CLI or Azure Resource Manager templates, or call it from a pipeline with the DevOps task (preview). The portal experience has been available since March 2023. Because the template is an Azure resource, the build definition can live in source control next to your other infrastructure code; that is an operating choice rather than a feature of the service.

Two routes: custom image templates or a direct template

Learn's custom image templates article says they are built on Azure Image Builder and tailored for Azure Virtual Desktop. The template is a JSON file with the source image, distribution targets, build properties and customizations, which is submitted to Azure Image Builder.

AspectAVD custom image templatesDirect Azure Image Builder template
Where you workAzure Virtual Desktop, Custom image templates, +Add custom image templatePowerShell, CLI, ARM templates or a pipeline task
CustomizationsBuilt-in scripts plus your own scriptsAny customizers the template format supports
Built-in AVD scriptsLanguage packs, time zone redirection, FSLogix, RDP Shortpath, Teams optimizations, session timeouts, Windows Updates and moreYou write or reuse the scripts yourself
OutputManaged image, Azure Compute Gallery or bothManaged image, Azure Compute Gallery or VHD
Start a buildStart build in the portal listRun action on the template resource

The built-in scripts are stored in the Azure RDS-Templates GitHub repository. Custom scripts must be at a publicly available location, or in a storage account where the managed identity has an RBAC role such as Storage Blob Data Reader.

Prerequisites

For custom image templates, Learn's creation article lists:

  • Registered resource providers: Microsoft.DesktopVirtualization, Microsoft.VirtualMachineImages, Microsoft.Storage, Microsoft.Compute, Microsoft.Network, Microsoft.KeyVault and Microsoft.ContainerInstance.
  • A resource group for templates and images. If you bring your own staging group for the build, it must be empty before the build starts.
  • A user-assigned managed identity, which Learn recommends creating specifically for custom image templates, with a custom role. The role's actions are read on galleries, images and image versions, write on image versions, and read, write and delete on managed images. Scope the assignment appropriately, ideally to the resource group you use to store the templates. If you pick a different resource group for a managed image on the distribution targets tab, the identity needs the same role assignment there.
  • Optionally an Azure Compute Gallery with an image definition. Do not create a version in advance.
  • Optionally a virtual network. If it uses a private service policy, the policy must be disabled for Image Builder to work.

The VM Image Builder and PowerShell article lists five providers for a direct template: VirtualMachineImages, Storage, Compute, KeyVault and ContainerInstance.

Anatomy of a template

Learn's template reference gives these building blocks. The fragment below uses its property names with placeholders. It is an illustration, not a tested template, and it is not deployable as it stands: the reference's basic format also wraps the properties in type (which must be Microsoft.VirtualMachineImages/imageTemplates), location, optional tags and identity, and the deployment supplies an API version.

"properties": {
  "buildTimeoutInMinutes": 120,
  "vmProfile": { "vmSize": "Standard_D2ds_v4", "osDiskSizeGB": 0 },
  "source": {
    "type": "PlatformImage",
    "publisher": "MicrosoftWindowsDesktop",
    "offer": "<offer>",
    "sku": "<generation 2 multi-session sku>",
    "version": "latest"
  },
  "customize": [
    { "type": "PowerShell", "name": "install-apps", "runElevated": true, "runAsSystem": true, "inline": [ "<commands>" ] },
    { "type": "WindowsRestart" },
    { "type": "WindowsUpdate" }
  ],
  "distribute": [
    {
      "type": "SharedImage",
      "galleryImageId": "<gallery image definition resource ID>",
      "runOutputName": "avdOutput",
      "versioning": { "scheme": "Latest", "major": 1 },
      "targetRegions": [ { "name": "<region>", "replicaCount": 2, "storageAccountType": "Standard_ZRS" } ]
    }
  ]
}

Points from the reference:

  • Source. Three types: PlatformImage, ManagedImage (same subscription and region) and SharedImageVersion. Only generalized source images are supported. The overview shows az vm image list --publisher MicrosoftWindowsDesktop --sku g2 --output table --all for generation 2 SKUs.
  • Build timeout. If you omit it or set 0, the default is 240 minutes. The minimum is 6 and the maximum 960. For Windows Learn does not recommend less than 60.
  • Build VM. The default is Standard_D1_v2 for generation 1 and Standard_D2ds_v4 for generation 2. The PowerShell walkthrough says the D1_v2 default is not suitable for Windows and recommends Standard_D2_v2 or greater. A value of 0 for osDiskSizeGB keeps the source size; you cannot reduce it.
  • Customizers. Customizers run in the order they appear in the template. The types are File, PowerShell, Shell, WindowsRestart and WindowsUpdate. The File customizer suits downloads under 20 MB. For the WindowsUpdate customizer, Microsoft tests and validates the community provisioner and investigates issues with it, but the open-source project itself is not officially supported.
  • Distribution. ManagedImage, SharedImage and VHD, each with a unique runOutputName. With automatic versioning, scheme Latest increments the version and major pins the major number. Learn's example: with versions up to 2.1.0 in the gallery, major 1 yields 1.2.1. replicationRegions is deprecated from API version 2022-07-01 in favour of targetRegions.
  • Error handling. onCustomizerError and onValidationError accept cleanup or abort. With abort, the service skips cleanup and you remove the temporary resources yourself.

Rules for Windows build scripts

The PowerShell walkthrough lists tips for Windows builds. Set runElevated and runAsSystem to true on PowerShell customizers, or, in Learn's words, the build will stop responding. Use write-host so output reaches customization.log. Scripts must exit with 0, because a non-zero code fails the customization and stops the build. Test scripts on a standalone VM first. The same page says its sample scripts are for illustration and testing only, not for production workloads.

Deploy and start the build

  1. Submit the template. The PowerShell article uses New-AzResourceGroupDeployment with an ARM template file and parameters. The identity block is required: the reference says you must create a user-assigned identity with permissions on the resources, and list it under identity as "type": "UserAssigned" with its resource ID. Keep secrets out of the template: inline commands can be viewed in the template definition, so the reference says passwords, SAS tokens and authentication tokens belong in scripts in Azure Storage, where access requires authentication.
  2. Optionally validate. The validate property validates platform images and customized images. With sourceValidationOnly set to true, the image in source is validated directly and no separate build runs. If validation fails, continueDistributeOnFailure decides whether the image is still distributed; the reference advises caution with true.
  3. Start the build. Either set autoRun to Enabled, which starts one build when the template is created, or invoke Run. The reference gives:
Invoke-AzResourceAction -ResourceName $imageTemplateName -ResourceGroupName $imageResourceGroup -ResourceType Microsoft.VirtualMachineImages/imageTemplates -ApiVersion "2023-07-01" -Action Run -Force
az resource invoke-action --resource-group $imageResourceGroup --resource-type Microsoft.VirtualMachineImages/imageTemplates -n <template-name> --action Run

autoRun does not give ongoing builds, so a rebuild needs another Run. In the portal, select the template in Custom image templates and choose Start build.

  1. Watch the status. Start-AzImageBuilderTemplate -NoWait returns immediately. Get-AzImageBuilderTemplate exposes LastRunStatusRunState, LastRunStatusMessage and LastRunStatusRunSubState.
  2. Use the result. In the host pool wizard, select See all images, then My Items, and choose My Images for managed images or Shared Images for a gallery. The VM size must match the image generation.

Where the logs are

By default the service creates a staging resource group named IT_<ResourceGroupName>_<TemplateName>_<GUID>; you can name your own with stagingResourceGroup (or on the Build properties tab). Learn says most resources in it are temporary and deleted after the build, except the storage account. In that account, the packerlogs container has one folder per build, and each holds customization.log and, when validation ran, validation.log. The custom image templates troubleshooting page says entries for built-in scripts begin Starting AVD AIB Customization: {script name}. The Image Builder troubleshooting page gives the download path: storage account, Data Storage, the Container option, the packerlogs container, then the folder of the build you want. For a running build, open the container instance vmimagebuilder-build-container-... in the staging group, under Settings and Containers, on the Logs tab.

Limits, costs and recent changes

  • Azure Hybrid Benefit for Windows Server is not supported.
  • The image template is immutable and cannot be moved. To relocate it, recreate it with a new name.
  • Trusted Launch and Confidential VM: images marked TrustedLaunchSupported or ConfidentialVMSupported work as sources, but TrustedLaunch and ConfidentialVM images do not. Source and distribution must both be TrustedLaunchSupported.
  • Cost: compute, networking and storage at your Azure rates, plus possible egress to other regions.
  • Deleting the template: the overview suggests it to avoid the storage cost, but the reference says the staging resource group is deleted with the template, a VHD distribution is lost with it ("If you delete the image template, then you'll lose the VHD"), and the logs go with the group. Copy VHDs and download logs first. The custom image templates page says the IT_ group is also deleted when you delete the resource group containing the image, while the PowerShell walkthrough says that deleting only the resource group leaves the IT_ group behind. Deleting the template first avoids the question.
  • For Azure DevOps builds, the troubleshooting page says the staging group is deleted after a successful build, so reading a log needs a sleep step.
  • March 2026 release notes: after 31 March 2026 new virtual networks default to private subnets without default outbound access. If a template sets subnetId or containerInstanceSubnetId and the build or validation VMs need internet access, that subnet must have outbound access enabled. Separately, with Azure unmanaged disks retiring, VHDs produced by Image Builder can no longer provision new VMs.

Sources

Follow the showApple PodcastsSpotify

FAQ

Is Azure Image Builder the same thing as AVD custom image templates?

Not exactly. Microsoft Learn says custom image templates in Azure Virtual Desktop are built on Azure Image Builder and tailored for AVD. They give a portal flow with built-in scripts, while Azure Image Builder itself accepts any template you write.

Where do I find the build log if a customization fails?

Learn says the staging resource group holds a storage account with a packerlogs container. Each build has a folder there with customization.log, and validation.log when validation ran. Deleting the template removes the group and these logs.

Do I pay for Azure Image Builder?

Learn says you incur compute, networking and storage costs similar to building images manually, charged at your Azure rates, plus possible egress charges when the image is distributed to other regions.

Entities in this article

Topics of this article:

Azure Image Builder – Part of Microsoft Azure Azure Virtual Desktop – Cloud platform by Microsoft

Related articles

More articles on Azure Virtual Desktop:

RSS