Upgrade Packer JSON template to HCL2 for Azure Virtual Desktop

Reading Time: 3 minutes

Introduction

From my last blog post link https://alven.tech/azure-image-builder-or-packer-for-wvd/ , I used a JSON packer template, and I promised a new blog post regarding how to upgrade to HCL2.

HashiCorp recommends using the HCL2 as from version 1.7.0, so it’s about time, I finally do the switch!

The documentation Packer is really good, so I will basically just follow along with their documentation to see If I struck any challenges. https://learn.hashicorp.com/tutorials/packer/hcl2-upgrade

During this post, we will bring the JSON template and upgrade it. I will also try to improve the template since HCL gives us better options to document annotations.

Getting started

I’ve got an existing JSON file from GitHub below

from my terminal I use the command packer hcl2_upgrade -with-annotations .\Packer_w10_20h2_SIG.json

Successfully created .\Packer_w10_20h2_SIG.json.pkr.hcl. Exit 0

Time to review the HCL2 Configuration after the upgrade. I opend up the new *..pkr.hcl file in vs code to check the annotations, parameters, and variables. Working with the new template is way better and I didn’t have to change much other than the variables and Update the annotations to suit my needs.

Build the packer HCL2 template in Azure DevOps

In Github, I’ve added the raw configuration from the HCL2 upgrade, but I’ve changed the variables to my needs.

I’m using an Azure CLI task to test the new configuration template. Go back to my old blog https://alven.tech/azure-image-builder-or-packer-for-wvd/ if you need more details on how to configure the Azure DevOps Pipeline.

In the Inline Script, I will add the build shell script. I’m adding the variables with export and generating timestamp for one of the variables (image_version) to set the semantic version that’s required by SIG / Azure Compute Gallery. You can also use a variable file if you prefer setting your variables that way.

Voila! When running the build we can see that it’s started the azure-arm packer Build stage.

Summary

Upgrading the template was straightforward and I will for sure prefer working with the HCL configuration. If you have already been working with terraform the HCL configuration constructs more logic for you. That’s if for this short blog post.

Documentation

https://learn.hashicorp.com/tutorials/packer/hcl2-upgrade

https://www.packer.io/docs/templates/hcl_templates/variables#environment-variables

Leave A Comment

Your email address will not be published.