Search code examples
amazon-web-servicesamazon-amipacker

Is it possible to add a tag to AWS AMI created with Packer containing name of filtered source_ami?


I am building AWS AMI with Packer. The source image is defined by filter source_ami_filter and the source block looks like

source "amazon-ebs" "test-image" {
  ami_name      = "Some AMI Name"
  instance_type = "t2.micro"
  region        = "eu-central-1"
  source_ami_filter {
    filters = {
      name                = "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["099720109477"]
  }
  tags = {
//    source_ami_name = ...  # how to get it?
  }
  ssh_username = "ubuntu"
}

How to get the name of the used source AMI from canonical?


Solution

  • There are some shared information variables, for instance, SourceAMIName.

    That means you can get your source AMI name and use it as a tag by doing this:

    source_ami_name = "{{ .SourceAMIName }}"
    

    Here's a full example: https://www.packer.io/docs/builders/amazon/ebs#tag-example