Get AMI ID from a packer build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Complete example using HCL with a manifest post-processor and the jq command from @dougireton:
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
}
}
Then just:
jq -r '.builds[0].artifact_id|split(":")[1]' ./manifest.json
Expected output:
ami-0dcbaf6794c89f392
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
@CodeForcer yes, definitely I agree!!!