Could not parse template for following block: "template: generated:4: function "clean_resource_name" not defined"

Today I wanted to convert a json packer template to HCL, but ran into issues with the conversion around here:

#could not parse template for following block: "template: generated:4: function \"clean_resource_name\" not defined"

source "amazon-ebs" "amazon-linux-2-ami" {
  ami_description = "An Amazon Linux 2 AMI that will accept connections from hosts with TLS Certs."
  ami_name        = "firehawk-base-amazon-linux-2-{{isotime | clean_resource_name}}-{{uuid}}"
  instance_type   = "t2.micro"
  region          = "{{user `aws_region`}}"
  source_ami_filter {
    filters = {
      architecture                       = "x86_64"
      "block-device-mapping.volume-type" = "gp2"
      name                               = "*amzn2-ami-hvm-*"
      root-device-type                   = "ebs"
      virtualization-type                = "hvm"
    most_recent = true
    owners      = ["amazon"]
  ssh_username = "ec2-user"

what alternatives do I have to fix this line?

ami_name        = "firehawk-base-amazon-linux-2-{{isotime | clean_resource_name}}-{{uuid}}"

Hello there !

First of all, if you keep on using this value: "firehawk-base-amazon-linux-2-{{isotime | clean_resource_name}}-{{uuid}}" things will keep on working, but if you’d like something more “HCL2” I’d so something like that:

source "..." "..." {
  ami_name = "firehawk-base-amazon-linux-2-${ formatdate(YYYY-MM-DD'T'hh:mm:ssZ", timestamp()) }-${uuidv4()}"

For technical reasons, clean_resource_name cannot be re-implemented into Packer HCL2. The isotime is a golang templating specific function and to remain consistent with Terraform we did not import it over, HCL2 does have a timestamp which can be used alongside formatdate to get something better formatted. And if this builds once this should build forever.

Lastly, you could use custom validation rules in input variables to make sure the prefix is valid but since that prefix not meant to change much I wouldn’t do that.

Custom validation rules are going to be available in the next version of Packer. If you want I can give you an example here.

When I tried a local var and used it:

timestamp    = formatdate("YYYY-MM-DD'T'hh:mm:ssZ", timestamp())

it produced errors like:

[05:21:10] * 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.
[05:21:10]   on /home/ec2-user/environment/firehawk-main/modules/terraform-aws-vault/examples/bastion-ami/bastion.json.pkr.hcl line 40:
[05:21:10]   (source code not available)
[05:21:10] Error: 1 error(s) occurred:

I’m not sure why. This did work:

timestamp    = regex_replace(timestamp(), "[- TZ:]", "")

But is it equivalent?