Getting the Source AMI id when using HCL configuration

I’m looking to add the source AMI ID to a tag of a new AMI when using the HCL config format, is there an equivalent to using .SourceAMI yet? If that still works then please let me know how as I’ve not got anything working yet! :slight_smile:

Thanks

1 Like

Anyone know if this is possible yet?

Yes! here’s a working config with several examples:

(This is on v1.6.1 which dropped yesterday)

source "amazon-ebs" "basic-example" {
  region =  "us-east-1"
  source_ami_filter {
    filters = {
       virtualization-type = "hvm"
       name = "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*"
       root-device-type = "ebs"
    }
    owners = ["099720109477"]
    most_recent = true
  }
  instance_type =  "t2.small"
  ssh_username =  "ubuntu"
  ami_name =  "packer_AWS {{timestamp}}"
}

build {
  sources = [
    "source.amazon-ebs.basic-example"
  ]

  provisioner "shell-local"{
    inline = ["echo '${build.SourceAMIName}'"]
    environment_vars = ["HELLO_USER=packeruser", "SOURCE_AMI_NAME=${build.SourceAMIName}"]
    execute_command = ["/bin/sh", "-c", "echo '${build.SourceAMIName}'", "{{.Vars}} {{.Script}}"]
  }
}

edit: this example use SourceAMIName but it will also work for SourceAMI:

use ${build.SourceAMI} if embedding it in a string (most likely), or build.SourceAMI if you’re setting it as a standalone value in an option

That probably does work, but counter intuitively the tags for the AMI are set in the source definition where .SourceAMI isn’t currently available, https://www.packer.io/docs/builders/amazon/ebs#tag-example shows an example of setting a tag but I get:

This object does not have an attribute named "SourceAMI"

If I try:

  tag {
    key = "ami_id"
    value = "{{ .SourceAMI }}"
  }

Hmm. I think you’re going to have to define any tag that includes these specialized template engines inside the source rather than as standalone variables that you bring into the source. This works for me on v1.6.1:

source "amazon-ebs" "basic-example" {
  region = "us-east-1"
  source_ami_filter {
    filters = {
      virtualization-type = "hvm"
      name                = "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*"
      root-device-type    = "ebs"
    }
    owners      = ["099720109477"]
    most_recent = true
  }
  instance_type = "t2.small"
  ssh_username  = "ubuntu"
  ami_name      = "packer_AWS {{timestamp}}"
  tags = {
        Base_AMI_ID = "{{ .SourceAMI }}"
        Base_AMI_Name = "{{ .SourceAMIName }}"
    }
}

OK, not sure what I was doing wrong before, but that does work now, strange, but thanks for the help

So I tried to use the tag instead of the tags block so I can do a dynamic block with other fixed tags.

However, it looks like {{ .SourceAMI }} would return no value:

  dynamic "tag" {
    for_each = merge(
      var.my__other_tags,
      {
        Name       = "${var.ami_name} {{ isotime \"2006-01-02-15-04\" }}",
        source-ami = "{{ .SourceAMI }}"
      }
    )
    content {
      key  = tag.key
      value = tag.value
    }
  }

From the packer logs:

==> amazon-ebs.amzn2: Creating AMI tags
    amazon-ebs.amzn2: Adding tag: "Name": "Amazon Linux 2 AMI 2020-10-20-16-48"
    amazon-ebs.amzn2: Adding tag: "source-ami": "<no value>"
...

I’m using the latest Packer v1.6.4. Any ideas what I did wrong here?

Hi, thanks for reaching out.

I think this is a gap that still exists in HCL – I will work with the Packer team to try to get this fixed.

1 Like

Thanks @SwampDragons.

If there is a GitHub issue for this, could you link me to that so I can keep an eye on the progress?

For those who are interested in this issue. It looks like @SwampDragons has merged her fix in https://github.com/hashicorp/packer/pull/10224

Thank you again for fixing this!

1 Like