Skip to content

Instantly share code, notes, and snippets.

Last active June 20, 2023 15:38
  • Star 91 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Get AMI ID from a packer build
packer build packer.json 2>&1 | sudo tee output.txt
tail -2 output.txt | head -2 | awk 'match($0, /ami-.*/) { print substr($0, RSTART, RLENGTH) }' > sudo ami.txt
Copy link

wmbutler commented Apr 7, 2018

The manifest postprocessor worked great for me:

  "post-processors": [
        "output": "manifest.json",
        "strip_path": true,
        "type": "manifest"

Then this chain of events will capture the ami-XXXXXX as shown by @dayer4b above.

cat manifest.json | jq -r .builds[0].artifact_id |  cut -d':' -f2

Copy link

Aagat commented Jun 8, 2018

I ended up using this along with manifest post processor to get the latest ami

cat manifest.json | jq -r '.builds[-1].artifact_id' |  cut -d':' -f2

Copy link

How can we directly export the AMI_ID to global variable?

Copy link

This is old, but comes up when you search for how to get the ami id from the amazon-import post-processor. Note that the manifest will contain the AMI id, but you have to wrap the amazon-import post-processor and the manifest post-processor within the same list, or as Packer calls it, a sequence.

"post-processors": [
      "type": "amazon-import",
      "ami_name": "import_test",
      "region": "{{ user `target_region` }}",
      "s3_bucket_name": "{{ user `s3_artifact_bucket` }}",
      "role_name": "{{ user `aws_vmimport_role` }}"
      { "type":"manifest" }

More info here: hashicorp/packer#7297

Copy link

You can omit the cat command and pass the file file to jq directly or jq -r '.builds[-1].artifact_id' manifest.json | cut -d':' -f2

Copy link

Gist is one of those sites where it pays to read the comments. I was able to take this and combine it with some of the above additional techniques and wrap it together in a modern AWS demonstration. Enjoy:

Copy link

Using a post-processor with jq works as well.

jq -r '.builds[0].artifact_id|split(":")[1]' ./manifest.json

Copy link

CodeForcer commented Jul 2, 2021

Seriously, we shouldn't have to do all this parsing of text. Packer should just have a way to spit out the ami_id in the language itself so we can use it in future build stages without having to parse arrays in json manifest files

Copy link

@CodeForcer yes, definitely I agree!!!

Copy link

Complete example using HCL with a manifest post-processor and the jq command from @dougireton:

packer {
  required_plugins {
    amazon = {
      version = ">= 0.0.2"
      source  = ""

source "amazon-ebs" "ubuntu" {
  ami_name      = "custom-ami-{{timestamp}}"
  instance_type = "t2.micro"
  region        = "eu-west-1"
  source_ami_filter {
    filters = {
      name                = "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    most_recent = true
    owners      = ["099720109477"]
  ssh_username = "ubuntu"
  tags = {
    Name = "my-app"
    Os   = "ubuntu-jammy-22.04-amd64-server"

build {
  name = "custom-ami"
  sources = [""]
    post-processor "manifest" {
        output = "manifest.json"
        strip_path = true

Then just:
jq -r '.builds[0].artifact_id|split(":")[1]' ./manifest.json
Expected output:

But agree with @CodeForcer 2022 and packer still does not have a built-in method to spit out the id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment