Converting Packer templates from json to HCL for Packer version 1.7.2. The easiest thing to do is try the automated conversion:
packer hcl2_upgrade -output-file=template-converted.pkr.hcl template.json
For environment variables we need to set them with a PKR_VAR prefix e.g.
export PKR_VAR_IAM_ROLE=jonny-test
Then that can be defined in the template.pkr.hcl file with:
variable "iam_role" {
type = string
default = "${env("IAM_ROLE")}"
validation {
condition = length(var.iam_role) > 0
error_message = <<EOF
The IAM_ROLE environment variable must be set.
EOF
}
}
And then used in later sections like this:
source "amazon-ebs" "debian" {
ami_name = "my-debian-10-{{isotime `2006-01-02 15-04-05`}}"
assume_role {
role_arn = "${var.iam_role}"
session_name = "packer"
}
Then your validation should work:
packer validate template.pkr.hcl
And if you want to check the variables available when using the template:
packer console template.pkr.hcl
and at the prompt enter:
variables