packer-plugin-amazon
packer-plugin-amazon copied to clipboard
Can't use "clean_resource_name" in source
Overview of the Issue
The EBS builder plugin points to the template engine which seems to be a legacy json template per URL. To make matters worse, one of the functions mentioned there, and in the AMI name validation, clean_resource_name doesn't seem to be valid for HCL2 per this discuss post , and trying to use the same example mentioned in the template engine results in function "clean_resource_name" not defined error. This should be corrected, either by means of corrected doc link, or a reference to something else that can generate a unique ami_name
Sample template:
packer {
required_plugins {
amazon = {
version = ">= 1.1.1"
source = "github.com/hashicorp/amazon"
}
}
}
data "amazon-ami" "ubuntu" {
filters = {
virtualization-type = "hvm"
name = "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"
}
owners = ["099720109477"]
most_recent = true
region = "us-east-1"
}
source "amazon-ebs" "packer" {
region = "us-east-1"
source_ami = data.amazon-ami.ubuntu.id
instance_type = "m5a.large"
ssh_username = "ubuntu"
ami_name = "packer-build {{timestamp | clean_resource_name}}"
}
build {
sources = ["source.amazon-ebs.packer"]
provisioner "shell" {
inline = ["echo Hello from packer"]
}
}
Reproduction Steps
Steps to reproduce this issue
Try to run packer build . with the template mentioned above. This results in error
packer build .
Error: invalid 'ami_name': template: root:1: function "clean_resource_name" not defined in:
packer-{{timestamp | clean_resource_name }}
on source.pkr.hcl line 1:
(source code not available)
Plugin and Packer version
From packer version
packer version
Packer v1.8.3
Simplified Packer Buildfile
See above
Operating system and Environment details
OS, Architecture, and any other information you can provide about the environment.
Log Fragments and crash.log files
MacBook Pro (16-inch, 2021) running Apple M1 and macOS 12.6 (21G115)
Include appropriate log fragments. If the log is longer than a few dozen lines, please include the URL to the gist of the log or use the Github detailed format instead of posting it directly in the issue.
Set the env var PACKER_LOG=1 for maximum log detail.
See above
This issue still persists on Packer v1.10.1. For those who end up here searching for a fix the workaround:
"packer-ubuntu-aws-${regex_replace(timestamp(), "[^a-zA-Z0-9-]", "")}"
Still an issue in 1.12.0.
Error: invalid 'ami_name': template: root:1: function "clean_resource_name" not defined in:
amazon-eks-node-1.28-custom-{{timestamp | clean_resource_name}}
I've worked around it like this:
locals {
timestamp = regex_replace(timestamp(), "[ :]", "")
ami_target_name = "amazon-eks-node-${var.eks_version}-custom-${local.timestamp}"
https://github.com/HariSekhon/Packer/blob/main/aws_eks.amazonlinux2.x86_64.pkr.hcl