Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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