Adding a date suffix to generated image

Am trying to add a suffix of %Y-%m-%d suffix to the generated image. However I get an error about formatdate not defined error. Am using Packer v1.10.2


packer {
  required_plugins {
    arm-image = {
      version = ">= 0.2.5"
      source  = "github.com/solo-io/arm-image"
    }
  }
}


source "arm-image" "pirogue-os" {
  iso_checksum = "sha256:58a3ec57402c86332e67789a6b8f149aeeb4e7bb0a16c9388a66ea6e07012e45"
  iso_url      = "https://downloads.raspberrypi.com/raspios_lite_arm64/images/raspios_lite_arm64-2024-03-15/2024-03-15-raspios-bookworm-arm64-lite.img.xz"
}

build {
  source "source.arm-image.pirogue-os" {
    name = "pirogue-os-12"
  }

  post-processor "compress" {
    output              = "{{.BuildName}}-{{formatdate('YYYY-MM-DD',timestamp)}}.xz"
    compression_level   = 9
    keep_input_artifact = false
  }


  post-processor "checksum" {
    checksum_types = ["sha256"]
    output         = "{{.BuildName}}-{{formatdate('YYYY-MM-DD',timestamp)}}-{{.ChecksumType}}.checksum"
  }

}

Hi @arky,

There are two alternatives for string interpolation with HCL2: native HCL2, and legacy (inherited from JSON-only templates).

The legacy format is essentially what you’ve done with {{.BuildName}}, it’s a form of mustache/go templating with some keywords for accessing functions or user-provided variables. Please refer to our docs for more information on the subject.

If you’re using HCL2, legacy template interpolation is still allowed (and in some cases necessary for technical reasons), but is discouraged in favour of HCL native string interpolation with ${}.
In your case for invoking formatdate this is likely what you’re aiming for.

Typically an updated template for your use-case would look like this:

packer {
  required_plugins {
    arm-image = {
      version = ">= 0.2.5"
      source  = "github.com/solo-io/arm-image"
    }
  }
}

source "arm-image" "pirogue-os" {
  iso_checksum = "sha256:58a3ec57402c86332e67789a6b8f149aeeb4e7bb0a16c9388a66ea6e07012e45"
  iso_url      = "https://downloads.raspberrypi.com/raspios_lite_arm64/images/raspios_lite_arm64-2024-03-15/2024-03-15-raspios-bookworm-arm64-lite.img.xz"
}

build {
  source "source.arm-image.pirogue-os" {
    name = "pirogue-os-12"
  }

  post-processor "compress" {
    output              = "{{.BuildName}}-${ formatdate("YYYY-MM-DD", timestamp()) }.xz"
    compression_level   = 9
    keep_input_artifact = false
  }

  post-processor "checksum" {
    checksum_types = ["sha256"]
    output         = "{{.BuildName}}-${ formatdate('YYYY-MM-DD', timestamp()) }-{{.ChecksumType}}.checksum"
  }
}

Note: for reference, BuildName is still interpolated using legacy-JSON syntax since it’s runtime-level information that is interpolated by the plugins, which lack HCL2 interpolation capabilities.

1 Like

Thank you @lbajolet-hashicorp That worked perfectly.


  post-processor "compress" {
    output              = "{{.BuildName}}-${ formatdate("YYYY-MM-DD", timestamp() ) }.xz"
    compression_level   = 9
  }

Here are some things I didn’t find answers for:

  • Is there a HCL2 way to access {{.BuildName}} ? Could I get ${build.source.name} directly?
  • Is there reason why formatdate("YYYY-MM-DD", timestamp()) doesn’t accept single quotes (’ ') ?
  • Is there a way to declare a local variable like bundle_suffix instead of repeating the formatdate for each post processor ?

Hi again @arky,

Regarding your questions:

Is there a HCL2 way to access {{.BuildName}} ? Could I get ${build.source.name} directly?

$(source.name) is probably what you’re looking for here!

Is there reason why formatdate("YYYY-MM-DD", timestamp()) doesn’t accept single quotes (’ ') ?

I believe this is because of the HCL syntax, where ' are reserved for single characters while " is for strings.

Is there a way to declare a local variable like bundle_suffix instead of repeating the formatdate for each post processor ?

Yep, you can use local for this:

local "bundle_suffix" {
  expression ="${formatdate("YYYY-MM-DD", timestamp())}"
}

Then you can reference the variable wherever needed as ${local.bundle_suffix}.

1 Like

Thank you so much @lbajolet-hashicorp