packerhcl

Packer HCL2 validation returns "There is no function named formatdate"


I'm trying to assign a variable in a Packer v1.9.4 HCL2 template:

variable "timestamp" {
  type = string
  default = formatdate("YYMMDDhhmmss", timestamp())
}

When I validate it I get the error in the subject:

Error: Call to unknown function

  on C:\Users\mathe\Documents\Azure Files\Packer Templates\avd-packer-win10-proto.pkr.hcl line 19, in variable "timestamp":
19:   default = formatdate("YYMMDDhhmmss", timestamp())

There is no function named "formatdate".

I'm confused because the Packer documentation specifically calls out the formatdate function.

Has the formatdate function been removed or replaced? Am I missing a plugin or something?

Aside, is there a better way to assign a variable like this without using default?


Solution

  • Short answer is you can't call methods for Default values, or any kind of calculation / interpolation to result in the value passed. Defaults must be constants. Only local variables allow calculated values. It's a business rule set by Hashicorp for security reasons, I believe.

    This thread might help for context:

    https://github.com/hashicorp/packer/issues/9430