Image Template

Overview

The Image Template builder is used to create Image Templates for generating VM images. An image template starts with a source image, runs several customizations, and then can finally distribute the image to one or more destinations, including an Azure Image Gallery, a managed image, or a VHD file.

  • Image Templates (Microsoft.VirtualMachineImages/imageTemplates)

Builder Keywords

Applies ToKeywordPurpose
imageTemplatenameSets the name of the App Insights instance.
imageTemplateadd_identityAdds a managed identity to the the imageTemplate that it uses to access resources when building the image and to publish images to a gallery.
imageTemplatebuild_timeoutTimeout for the image builder, after which it will fail. Default is 4 hours.
imageTemplatesource_platform_imageSpecify the source image to be customized from the Azure Gallery.
imageTemplatesource_managed_imageSpecify the source as an existing managed image.
imageTemplatesource_shared_image_versionSpecify the source image to be customized from a shared image gallery.
imageTemplateadd_customizersAdd customizers to make changes to the image being built.
imageTemplateadd_distributorsSpecify one or more output distributions for the newly built image.
imageTemplateadd_tagsAdd tags to the image template resource.
imageTemplatedepends_onAdd explicit dependencies for the imageTemplate resource.
fileCustomizernameName for the file customizer as shown in logs.
fileCustomizersource_uriLocation to download the file.
fileCustomizerdestinationLocation to save the downloaded file - needs to be under /tmp on Linux or an existing location on Windows.
fileCustomizerchecksumThe SHA 256 checksum to validate the file.
shellCustomizernameThe name of the customizer as shown in logs.
shellCustomizerinline_statementsA list of shell commands to run.
shellScriptCustomizernameThe name of the customizer as shown in logs.
shellScriptCustomizerscript_uriA script to download and run.
shellScriptCustomizerchecksumThe SHA 256 checksum to validate the file.
powerShellCustomizernameThe name of the customizer as shown in logs.
powerShellCustomizerinline_statementsA list of PowerShell commands to run.
powerShellCustomizerrun_as_elevatedRun the commands in an elevated PowerShell session.
powerShellCustomizerrun_as_systemRun the commands that need to execute as the System.
powerShellCustomizervalid_exit_codesA list of exit codes to treat as successful.
powerShellScriptCustomizernameThe name of the customizer as shown in logs.
powerShellScriptCustomizerscript_uriA PowerShell script to download and run.
powerShellScriptCustomizerchecksumThe SHA 256 checksum to validate the file.
powerShellScriptCustomizerrun_as_elevatedRun the commands in an elevated PowerShell session.
powerShellScriptCustomizerrun_as_systemRun the commands that need to execute as the System.
powerShellScriptCustomizervalid_exit_codesA list of exit codes to treat as successful.
windowsRestartCustomizerrestart_commandCommand to execute the restart (optional). The default is ‘shutdown /r /f /t 0 /c "packer restart"’
windowsRestartCustomizerrestart_check_commandCommand to check if restart succeeded (optional).
windowsRestartCustomizerrestart_timeoutRestart timeout specified as a string of magnitude and unit. For example, 5m (5 minutes) or 2h (2 hours). The default is: 5m.
windowsUpdateCustomizersearch_criteriaOptional, defines which type of updates are installed (like Recommended or Important), BrowseOnly=0 and IsInstalled=0 (Recommended) is the default.
windowsUpdateCustomizerfiltersOptional, allows you to specify a filter to include or exclude updates.
windowsUpdateCustomizerupdate_limitOptional, defines how many updates can be installed, default 1000.
managedImageDistributorimage_idTarget image ID of the image to build. The resource group should exist and be accessible. Either ‘image_id’ or ‘image_name’ must be set.
managedImageDistributorimage_nameTarget image name of the image to build in the same resource group. Either ‘image_id’ or ‘image_name’ must be set.
managedImageDistributorlocationAzure region where the managed image should be created (required).
managedImageDistributorrun_output_nameA label for the run, shown in logs and in the portal.
managedImageDistributoradd_tagsAn optional list of tags that will be added on the image.
sharedImageDistributorgallery_image_idTarget ID for the gallery image to create. It can reference the image itself or a specific version to create.
sharedImageDistributoradd_replication_regionsAzure regions where the managed image should be replicated. First in the list should be the location of the image gallery.
sharedImageDistributorexclude_from_latestOption to ensure the image will not be the “latest” version so it will always be pulled by version number.
sharedImageDistributorrun_output_nameA label for the run, shown in logs and in the portal.
sharedImageDistributoradd_tagsAn optional list of tags that will be added on the gallery image.
vhdDistributorrun_output_nameA label for the run, shown in logs and in the portal.
vhdDistributoradd_tagsAn optional list of tags that will be added on the virtual hard disk.

Example

open Farmer
open Farmer.Builders

imageTemplate {
    name "Ubuntu2004WithJava"
    add_identity msi
    source_platform_image Vm.UbuntuServer_2004LTS

    add_customizers
        [
            shellCustomizer {
                name "install-jdk"

                inline_statements
                    [
                        "set -eux"
                        "sudo apt-get update"
                        "sudo apt-get -y upgrade"
                        "sudo apt-get -y install openjdk-17-jre-headless"
                    ]
            }
        ]

    add_distributors
        [
            sharedImageDistributor {
                gallery_image_id
                    (ResourceType("Microsoft.Compute/galleries/images", "2020-09-30")
                        .resourceId (
                            ResourceName "my-image-gallery",
                            ResourceName "java-server-os"
                        )
                    )
                add_replication_regions [ Location.EastUS ]
                add_tags [
                    "image-type", "java"
                ]
            }
        ]
}