Skip to content

Instantly share code, notes, and snippets.

@nmarley
Last active February 11, 2021 00:28
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 nmarley/216c0f4e8e6d768de3f00c45548ed6d1 to your computer and use it in GitHub Desktop.
Save nmarley/216c0f4e8e6d768de3f00c45548ed6d1 to your computer and use it in GitHub Desktop.
AWS EC2: Search most recent Ubuntu AMI for all regions
#! /bin/bash
# search based on prefix and most recent date
IMAGE_PREFIX="ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-"
# Canonical (makers of Ubuntu)
OWNER_ID="099720109477"
for REGION in $(aws ec2 describe-regions | jq -r '.Regions[].RegionName'); do
IMAGE_ID=$(aws ec2 describe-images --region "${REGION}" --filters "Name=name,Values=${IMAGE_PREFIX}*" --owners "$OWNER_ID" | jq -r '.Images | sort_by(.CreationDate)[-1].ImageId')
echo "${REGION}:"
echo " AMI: ${IMAGE_ID}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment