Skip to content

Instantly share code, notes, and snippets.

@sebbrandt87
Forked from irgeek/README.md
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save sebbrandt87/023b67e889940f2af094 to your computer and use it in GitHub Desktop.

Select an option

Save sebbrandt87/023b67e889940f2af094 to your computer and use it in GitHub Desktop.

Finding Packer-generated AMIs automatically after builds

The basic technique is to have Packer add a tag with a unique value during the build, and use AWS' built-in filtering capabilities to find that specific AMI after the build finishes.

  • template.json - Shows the settings that need to be added to your template
  • build.sh - Shows how to use the template to do a build and retrieve the AMI information
#!/bin/bash
# Generate a unique identified for this build. UUID is the easiest
UUID=$(uuid -v4)
# Pass the unique identifier to packer as a user variable
packer build -var build_uuid=${UUID} template.json
# Use the AWS CLI tools to query for AMIs, using the unique tag value to
# filter for the build we just did
aws ec2 describe-images --filters Name=tag:BuildUUID,Values=${UUID} > build.json
# Assuming you have `jq` installed (and you should if you're dealing with JSON)
AMI_ID=$(jq --raw-output '.Images[0].ImageId' build.json)
{
"variables": {
"build_uuid": null
},
"builders": [
{
"type": "amazon-ebs",
"tags": {
"BuildUUID": "{{user `build_uuid`}}"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment