Skip to content

Instantly share code, notes, and snippets.

@galvarado
Created December 2, 2022 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galvarado/61e5861907a0a60b1d848a7158b489d4 to your computer and use it in GitHub Desktop.
Save galvarado/61e5861907a0a60b1d848a7158b489d4 to your computer and use it in GitHub Desktop.
Get AMI ID from a packer build
Complete example to get the ami_id from a packer build using HCL sintaxis with a manifest post-processor and jq.
The manifest post-processor writes a JSON file with a list of all of the artifacts packer produces during a run
`
packer {
required_plugins {
amazon = {
version = ">= 0.0.2"
source = "github.com/hashicorp/amazon"
}
}
}
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 = ["source.amazon-ebs.ubuntu"]
post-processor "manifest" {
output = "manifest.json"
strip_path = true
}
}
`
Build the image:
`
$ packer build ami.pkr.hcl
`
Then just:
`
$ jq -r '.builds[0].artifact_id|split(":")[1]' ./manifest.json
`
Expected output:
`
ami-0dcbaf6794c89f392
`
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