Convert Packer JSON File to HCL2

Packer has announced that classic JSON files are now supported as legacy files. The limitations within classic JSON make it harder to configure the packer template. That’s why Hashicorp has decided to create HCL support for Packer. This blog is about how to convert your Packer JSON File to HCL2 (Hashi Corp Language 2).

Furthermore, JSON is not completely out of the picture. You can still use these files with the json.pkr file extension. For my packer files I am moving to HCL2 since this is more easy to configure. In addition, you could compare this to Bicep versus ARM Templates.

In this blog I will convert the Packer JSON File to HCL2 using just a Windows 10 machine. I use Powershell to do so. I will convert the default Packer Template as an example.


Windows 10 machine/VM

Download Packer here.

Converting the JSON template

Firstly, you need to download packer, you can do that here.

After that, unzip the downloaded package and copy that to a folder. I use C:\temp:

Convert Packer JSON File to HCL2 - Packer executable

Next up is locating the Packer Template, for the example I will put this in the same folder as the packer executable.

Like this:

Convert Packer JSON File to HCL2 - Packer files

If you need an empty template, you can find this default template in my Github here.

Next up is opening Powershell and navigating the folder which holds Packer and the template file:

After that, run the following line of code (use accordingly to your locations):

.\packer.exe hcl2_upgrade -with-annotations .\PackerTemplate.json

The conversion runs and you see that the conversion has completed:

NOTE: The file ends with .json.pkr.hcl, just to avoid confusion, remove the .json part of the file.

NOTE: Some complex configurations don’t convert properly, handle with care.

And you end with these files in your C:\Temp folder:

This is part of the syntax of the converted file:



Other Posts:
Windows 10 Image Series
Azure Subscription Select Menu in Powershell

Leave a Comment