Create Virtual Machine Image using Packer
Date published:
Azure Image Builder is a virtual machine image service on azure created by HashiCorp Packer. It designs to create and maintain virtual machines for consistent deployments. The image builder is automation that can be used with AKS with Azure pipeline and Azure Virtual Desktop to manage update and Legacy workloads to convert deployment to the pipeline. Packer is a command-line tool used to build VM or images using JSON or HashiCorp Configuration Language (HCL2) template file.
The main concept of the Azure Image builder can be shown below
- Produce an image version
- Store it in the Azure Compute Gallery - Which can be known as a shared gallery
- Replicate the image inside or across the different Azure region
- Deploy virtual machine or Virtual Machine Scale Sets from the image version.
The Image Definition is a resource of the Compute Gallery in the location Microsoft.Compute/galleries/Images. You can use it to specify the Image name, publisher, Offer/SKY, OS Type and the Recommended allowed VM spec etc.
The Image Template creates the image to include details such as the source image properties, customisation, and distribution object (Resource ID for image Definition and the Replication in Compute Gallary).
You can build the Packer image through the Azure Portal, Powershell or by Bicep.
This tutorial will show you how to use packer to create a Virtual Machine Image using Azure Powershell.
First, you need to create a resource group to store all the resources.
$rgName = “PackerGroup” $location = “UKSouth” New-AzResourceGroup -Name $rgName -Location $location
Create a Service Principal for the packer to authenticate to azure. In this example, I will be calling the service principle as PackerSPTest. You control the permissions as to what operations the service principal can perform in azure. Once you run the command, make a note of the details, and we will use it afterwards.
az ad sp create-for-rbac –role Contributor –query “{ client_id: appId, client_secret: password, tenant_id: tenant }”
You will need to get your Azure subscription ID. You can do this by running Get-AzSubscription.
The next step is to install the packer tool on your computer. To install packer, you can go to https://www.packer.io/downloads and find the correct OS for your system. In this case, I will be using Mac to download packer.
Once you have downloaded packer on your system, create a file windows.json and paste the script from the Windows Image Builder Powershell link at the bottom of this guide. The Windows.json script will create the resources for the image. Below is a table that will assist you in completing the detail for the script.
client_id - This is the appid client_secret - auto-generated password tenant_id Output from $sub.TenantId command subscription_id - The output result from the Get-AzSubscription
Once you have completed the whole script, you need to run it using the build command. It will create several resources using AzureRM operation, which will take several minutes to complete. Head back into the Azure portal, and you will find the image created in your selected resource group.
packer build ./windows.JSON
Further Reading :