Timestamp() template function

I’m looking at converting some packer JSON configuration files to HCL2. I have been playing with the packer hcl2_upgrade command with release 1.7.2 and have had success building an image with a new configuration file.

packer is recommending that a string like "{{isotime \"2006-01-02\"}}" be changed to make use of the timestamp and formatdate functions. I found the example on the timestamp page but it doesn’t seem to work for me the same way:

$ packer console
> timestamp()                         # this is the statement I entered
> timestamp()                         # this is the response
> "{{timestamp()}}"
???????: Interpolation error

template: root:1: unexpected "(" in operand



> "{{timestamp}}"
> "1618833849"
> exit
$

I’m not even sure if I should be using packer console to play with expressions like this. Am I doing something wrong?

Oh!!! It looks like packer console does not use work in hcl2 by default!

$ packer console --config-type=hcl2
> timestamp()
> 2021-04-19T12:13:52Z
1 Like

Hey there,
To run HCL2 functions in the packer console you need to specify that when starting the console, like :

$ packer console --config-type=hcl2

Anyways, another option to what you’re looking for is the legacy_isotime function. You can use like:

$ packer console --config-type=hcl2
> legacy_isotime("2006-01-02")         
> 2021-04-19  # output

You figured out by yourself while I was replying. :laughing:

1 Like