Hi,
I’m giving a first try to switch to HCL for my packer configuration.
This is mostly lead by the need to have some conditional, and readability, and common languages with our terraform stacks, and… so on
I’d like to have the following, which would let me easily switch between the creation of an amd64 / arm64 ami creation, without having to maintain multiple repos.
data "amazon-ami" "ami" {
filters = {
name = local.source_ami
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["amazon"]
region = var.region
}
where my vars in variables.pkr.hcl
are
variable "platform" {
description = "Target platform to build on. Must be in (linux-amd64, linux-arm64). Defaults to linux-amd64."
type = string
default = "linux-amd64"
validation {
condition = contains(["linux-amd64", "linux-arm64"], var.platform)
error_message = "We currently only support linux-amd64 and linux-arm64."
}
}
locals {
source_ami = var.platform == "linux-amd64" ? "amzn2-ami-hvm-*-x64_64-gp2" : "amzn2-ami-hvm-*-arm64-gp2"
}
But this fail with the following
Error: Unsupported attribute
on main.pkr.hcl line 7:
(source code not available)
This object does not have an attribute named "source_ami".
Am I missing something or is the interpolation not permitted in ami name filter ?
I tried both interpolation and use of a full variable (like above), no success.
I can use it everywhere else, but not here.
Sidenote : I was on packer 1.7.0
and tried to update to 1.7.4
but I get the same error.
Sidenote2 : If I skip the variable and put the string to the filter value, it works perfectly.