Formatdate not working

Using 1.6.1 and even 1.7.10 we are trying to name our AMI’s in AWS using this statement as part of a larger json file:

“ami_name”: “xx-amazon-windows-2022-full-{{formatdate(‘YYYY-MM-DD’T’hh:mm:ssZ’,timestamp)}}”,

But when we run it we get:

formatdate command not defined

I thought formatdate was built-in?

Do you have to do some sort of import like python or packer init function to make built-in functions work?

Hi :wave:

From your example it appears that you are trying to use the formatdate function inside of a JSON template. The formatdate function is a HCL2 template only function. Which is why Packer is responding with the undefined error.

If you are using JSON you will want to use the isotime function. You can find an example on the Packer website Template Engine - Templates | Packer by HashiCorp

Yes, we are trying to use a json file as our template for packer.

We are running with 1.6.1 of packer.

When we try {{isotime}} in ami name we get:

  • AMIName should only contain alphanumeric characters, parentheses (()), square
    brackets (), spaces ( ), periods (.), slashes (/), dashes (-), single quotes
    (’), at-signs (@), or underscores(_). You can use the clean_resource_name
    template filter to automatically clean your ami name.

When we try {{ isotime | clean_resource_name}} we get:
error interpolating default value for ‘ami_name’: template: root:1: function
“clean_resource_name” not defined

Does moving to 1.7.10 (latest) help?

Does moving to 1.7.10 (latest) help?

In this particular case it does not. But generally speaking upgrading to the latest version of Packer is always recommended to gain access to the latest improvements and bug fixes. If you don’t have a specific reason for sticking to 1.6.1. I would suggest downloading 1.7.10 and giving it a try.

When we try {{ isotime | clean_resource_name}} we get:
error interpolating default value for ‘ami_name’: template: root:1: function
“clean_resource_name” not defined

Thanks for providing the error when trying to interpolate the clean_resource_name function. You get this error when using the clean_resource_name function within a variable block. It is a little confusing but the function clean_resource_name can only be used within a builder block as each builder has its own rules on what a valid resource name can contain. This issue does a great job in explaining the problem you are running into.

That said, there are a few routes you can take.

  1. If not needed for a specific reason you can drop the variable and set the ami_name in the builder directly to “xx-amazon-windows-2022-full-{{isotime | clean_resource_name}}”.
    "builders": [
        {
            "type": "amazon-ebs",
            "access_key": "{{user `aws_access_key`}}",
            "secret_key": "{{user `aws_secret_key`}}",
            "ami_name": "xx-amazon-windows-2022-full-{{isotime | clean_resource_name}}",
            "region": "{{ user `region`}}", 
            "other fields": "..."
         }
  1. If you want to use a variable so that you can reuse the value of ami_name you can use the formatting option for the isotime function to give you the UTC time
"variables" {
  "ami_name": "xx-amazon-windows-2022-full-{{isotime \"2006-01-02T03-04-05Z\"}}"
}
  1. Upgrade your version of Packer to the latest version and convert the JSON template to HCL using the packer hcl2_upgrade command. Then you can use the formatdate and timestamp functions, along with the latest HCL features. The hcl2_upgrade command will take your JSON file and automatically convert the JSON blocks to their HCL2 equivalents, without overwriting the original source.