»hcl2_upgrade Command

The packer hcl2_upgrade Packer command is used to transpile a JSON configuration template to it's formatted HCL2 counterpart. The command will return a zero exit status on success, and a non-zero exit status on failure.

Example usage:

$ packer hcl2_upgrade my-template.json

Successfully created my-template.json.pkr.hcl

»Upgrading variables file

From v1.7.1, the hcl2_upgrade command can upgrade a variables file.

{
  "variables": {
    "aws_region": null,
    "aws_secondary_region": "{{ env `AWS_DEFAULT_REGION` }}",
    "aws_secret_key": "",
    "aws_access_key": ""
  },
  "sensitive-variables": ["aws_secret_key", "aws_access_key"]
}

»Go template functions

hcl2_upgrade will do its best to transform your go template calls to HCL2, here is the list of calls that should get transformed:

The rest of the calls should remain go template calls for now, this will be improved over time.

»Options

  • -output-file - File where to put the hcl2 generated config. Defaults to JSON_TEMPLATE.pkr.hcl
  • -with-annotations - Adds helper annotations with information about the generated HCL2 blocks.

»User variables using other user variables

Packer JSON recently started allowing using user variables from variables. In HCL2, input variables cannot use functions nor other variables and are virtually static, local variables must be used instead to craft more dynamic variables.

For v1.7.0 and lower, hcl2_upgrade doesn't upgrade variables to local variables, and it is up to you to upgrade them manually. Upgrade to v1.7.1 to let the command do it automatically for you.

Here is an example of a local variable using a string input variables:

variable "foo" {
  default = "Hello,"
}

variable "bar" {
  default = "World!"
}

locals {
  baz = "${var.foo} ${var.bar}"
}