I tried to Upgrade JSON Template to HCL2 for Packer

2022.01.12

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

What is HCL2:

HCL Stands For HashiCorp Configuration Language -it is an open source library maintained and made by HashiCorp. this language oriented towards configuration just like JSON and YAMl

Why to upgrade from JSON:

  • There are quotes everywhere in JSON, and it can make it a bit hard to parse the text as a human. HCL2 has much less quotes. And it uses quotes only when you want to set strings — so not fields — making the parsing a little bit easier on the eyes.
  • In HCL2 we can add comments
  • In HCL2 Building blocks can be split in files
  • In HCL2 we can add Stings in multiple line
variable "long_key" {
type = "string"
default = <<EOF
This is a long key.
Running over several lines.
It could be super handy for a boot_command.
EOF
}

Handson JSON -> HCL2:

You can use the hcl2_upgrade command to transition your existing Packer JSON template to HCL2

Prerequisites

  • packer

You can use the existing template or copy the bellow code and save it as xyz.json for testing

{
"builders": [
{
"type": "amazon-ebs",
"region": "ap-northeast-1",
"source_ami": "ami-1234567",
"instance_type": "t2.micro",
"ami_name": "DevelopersIO-try1",
"ssh_username": "ec2-user",
}
],
"provisioners":[
{
"type": "shell",
"inline": [
"sleep 30",
"sudo amazon-linux-extras install -y nginx1",
"sudo systemctl start nginx.service"
]
},
{
"type": "file",
"source": "index.html",
"destination": "/tmp/"
},
{
"type": "shell",
"inline": "sudo cp /tmp/index.html /usr/share/nginx/html/"
}
]
}

after saving a file Use the hcl2_upgrade command to upgrade the JSON template to HCL2

packer hcl2_upgrade -with-annotations xyz.json

new file written in HCl2 will  be created with name xyz.json.pkr.hcl

# This file was autogenerated by the 'packer hcl2_upgrade' command. We
# recommend double checking that everything is correct before going forward. We
# also recommend treating this file as disposable. The HCL2 blocks in this
# file can be moved to other files. For example, the variable blocks could be
# moved to their own 'variables.pkr.hcl' file, etc. Those files need to be
# suffixed with '.pkr.hcl' to be visible to Packer. To use multiple files at
# once they also need to be in the same folder. 'packer inspect folder/'
# will describe to you what is in that folder.

# Avoid mixing go templating calls ( for example ```{{ upper(`string`) }}``` )
# and HCL2 calls (for example '${ var.string_value_example }' ). They won't be
# executed together and the outcome will be unknown.

# source blocks are generated from your builders; a source can be referenced in
# build blocks. A build block runs provisioner and post-processors on a
# source. Read the documentation for source blocks here:
# https://www.packer.io/docs/templates/hcl_templates/blocks/source
source "amazon-ebs" "autogenerated_1" {
ami_name = "DevelopersIO-try1"
access_key = "wrongaccesskey"
instance_type = "t2.micro"
region = "ap-northeast-1"
secret_key = "WrongSecretkey"
source_ami = "ami-02d36247c5bc58c23"
ssh_username = "ec2-user"
}

# a build block invokes sources and runs provisioning steps on them. The
# documentation for build blocks can be found here:
# https://www.packer.io/docs/templates/hcl_templates/blocks/build
build {
sources = ["source.amazon-ebs.autogenerated_1"]

provisioner "shell" {
inline = ["sleep 30", "sudo amazon-linux-extras install -y nginx1", "sudo systemctl start nginx.service"]
}

provisioner "file" {
destination = "/tmp/"
source = "index.html"
}

provisioner "shell" {
inline = "sudo cp /tmp/index.html /usr/share/nginx/html/"
}

}

now we can initialise and build this file using below command

packer init .
packer build .

We have Created HCl2 Code from Json

Reference:

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

HCL2 Syntax