Restoring AMI with Packer

We have several options to discover an AMI id to use in infra as code resources, but with Packer from Hashicorp it is simple as define a block that will recover based on a pattern the id.

Above the solution using filter to restore latest image id for a ubuntu 20.04 version.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
source "amazon-ebs" "ubuntu" {
  ami_name      = "azure-agent-image-v0.1"
  instance_type = "t3.micro"
  region        = "eu-west-2"
  source_ami_filter {
    filters = {
      name                = "ubuntu/images/*ubuntu-*-20.04-amd64-server-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["099720109477"]
  }
  ssh_username = "ubuntu"
}

References