Working JSON breaks after HCL2 upgrade - Error: Variables not allowed

We’ve just updated to packer 1.7.0, and took the opportunity to migrate our JSON templates to HCL using packer hcl2_upgrade.

The upgrade went smoothly.

Unfortunately our build now breaks with this error:

Error: Variables not allowed

It appears that this is no longer allowed:

variable "template" {
  type    = string
  default = "${var.policy_name}-${var.release_version}-${var.os}-${var.os_version}-${var.os_arch}"

It also appears an approach we used in Terraform made its way into the Packer JSON, but now this use of local too is verboten:

variable "version" {
  type    = string
  default = "${local.timestamp}"

Obviously we can revert to JSON, but in case the fix is painless we’d rather switch now.

Appreciate any hints or tips.

Kind regards

We’ve had to revert to JSON to keep momentum.

We can’t see that we loose any functionality other than more convenient comments.

For JSON we use “_comment” items and filter these out prior to passing the template to Packer/Terraform.

Interested to hear other experiences.

Hello! This sounds like a bug to me. Do you mind opening an issue on GitHub so that the team can investigate and fix the hcl2_upgrade command? I would be very useful if you share the variables from the JSON template as well.

Meanwhile, you can make these variables as locals and this should be enough to fix your template.

locals {
   template = "${var.policy_name}-${var.release_version}-${var.os}-${var.os_version}-${var.os_arch}"
   timestamp = regex_replace(timestamp(), "[- TZ:]", "")
   version = local.timestamp

and refer to them later as local.version and local.template. Let me know if it works for you.

I was reading again our docs and this is actually the expected behavior and should upgrade the variables to local as I suggested. See: packer hcl2_upgrade - Commands | Packer by HashiCorp

Anyway, I think it worths having a look into this, maybe we can change that and make it easier by creating locals straight forward.

Hello, me again!
FYI - I opened a PR trying to address the issue you had here Allows hcl2_upgrade variables json file by sylviamoss · Pull Request #10676 · hashicorp/packer · GitHub

1 Like

@sylviamoss your PR is great - thank you.
It relates to a slightly different error - converting variables only JSON file.

This issue arose from a ‘complete’ packer template (JSON).
Continuing the conversation in issue #10681

1 Like

As best I can tell this issue will have been resolved in Packer release 1.7.1.