Skip to content

Instantly share code, notes, and snippets.

@defanator
Last active October 20, 2022 11:38
Show Gist options
  • Save defanator/0d7d44cf0654b5f6ae697eb08fb0d608 to your computer and use it in GitHub Desktop.
Save defanator/0d7d44cf0654b5f6ae697eb08fb0d608 to your computer and use it in GitHub Desktop.
Find AWS AMI images of popular distros
#!/bin/bash
LIMIT=1
usage() {
echo "usage: $(basename $0) [-l N] distro arch [arch...]" >&2
exit 1
}
rhel() {
version=$1
shift
for arch in $@; do
echo "=== RHEL $version $arch"
aws ec2 describe-images \
--owners 309956199498 \
--filters "Name=name,Values=RHEL-${version}*-${arch}*" | \
jq "[.Images[] | \
{ CreationDate, ImageId, Name } ] | \
sort_by(.CreationDate) | \
reverse | \
limit($LIMIT; .[])"
done
}
centos() {
family=$1
version=$2
shift 2
for arch in $@; do
echo "=== CentOS $version $arch"
aws ec2 describe-images \
--owners 125523088429 \
--filters "Name=name,Values=${family} ${version}*${arch}*" | \
jq "[.Images[] | \
{ CreationDate, ImageId, Name } ] | \
sort_by(.CreationDate) | \
reverse | \
limit($LIMIT; .[])"
done
}
fedora() {
version=$1
shift
for arch in $@; do
echo "=== Fedora $version $arch"
aws ec2 describe-images \
--owners 125523088429 \
--filters "Name=name,Values=Fedora-Cloud-Base-${version}-?.?.${arch}-hvm*gp2*" | \
jq "[.Images[] | \
{ CreationDate, ImageId, Name } ] | \
sort_by(.CreationDate) | \
reverse | \
limit($LIMIT; .[])"
done
}
_debian_codename() {
case "${1//./}" in
11) echo "bullseye" ;;
10) echo "buster" ;;
'*') echo "*" ;;
*) echo "unknown" ;;
esac
}
debian() {
version=$1
shift
codename=$(_debian_codename $version)
for arch in $@; do
echo "=== Debian $version $codename $arch"
aws ec2 describe-images \
--owners 136693071363 \
--filters "Name=name,Values=debian-${version}-${arch}*" | \
jq "[.Images[] | \
{ Name, CreationDate, ImageId } ] | \
sort_by(.CreationDate) | \
reverse | \
limit($LIMIT; .[])"
done
}
_ubuntu_codename() {
case "${1//./}" in
2204) echo "jammy" ;;
2004) echo "focal" ;;
1804) echo "bionic" ;;
1604) echo "xenial" ;;
'*') echo "*" ;;
*) echo "unknown" ;;
esac
}
ubuntu() {
version=$1
shift
codename=$(_ubuntu_codename $version)
for arch in $@; do
echo "=== Ubuntu $version $codename $arch"
aws ec2 describe-images \
--owners 099720109477 \
--filters "Name=name,Values=ubuntu/images/*ubuntu-${codename}-${version}-${arch}-server-*" | \
jq -r "[.Images[] | \
select(.RootDeviceType==\"ebs\") | \
{Name, CreationDate, ImageId}] | \
sort_by(.CreationDate) | \
reverse | \
limit($LIMIT; .[])"
done
}
amazonlinux() {
version=$1
shift
for arch in $@; do
echo "=== Amazon Linux $version $arch"
aws ec2 describe-images \
--owners 137112412989 \
--filters "Name=name,Values=amzn2-ami-hvm-${version}*-${arch}-gp"2 | \
jq "[.Images[] | \
{ CreationDate, ImageId, Name } ] | \
sort_by(.CreationDate) | \
reverse | \
limit($LIMIT; .[])"
done
}
almalinux() {
version=$1
shift
for arch in $@; do
echo "=== AlmaLinux $arch"
aws ec2 describe-images \
--owners 764336703387 \
--filter "Name=name,Values=AlmaLinux OS ${version}.*${arch}" | \
jq "[.Images[] | \
{ CreationDate, ImageId, Name, Description } ] | \
sort_by(.CreationDate) | \
reverse | \
limit($LIMIT; .[])"
done
}
rockylinux() {
version=$1
shift
for arch in $@; do
echo "=== RockyLinux $version $arch"
aws ec2 describe-images \
--owners 792107900819 \
--filter "Name=name,Values=Rocky-${version}*${arch}" | \
jq "[.Images[] | \
{ CreationDate, ImageId, Name, Description } ] | \
sort_by(.CreationDate) | \
reverse | \
limit($LIMIT; .[])"
done
}
while getopts l:h opt; do
case "$opt" in
l) LIMIT="$OPTARG" ;;
h) usage ;;
esac
done
shift $((OPTIND-1))
[ $# -lt 2 ] && usage
distro=$1
shift
case "$distro" in
rhel*)
v=${distro#rhel}
[ -n "$v" ] && rhel "$v" $@ || rhel '*' $@
;;
centosstream*)
v=${distro#centosstream}
[ -n "$v" ] && centos "CentOS Stream" "$v" $@ || centos "CentOS Stream" '*' $@
;;
centos*)
v=${distro#centos}
[ -n "$v" ] && centos "CentOS" "$v" $@ || centos "CentOS" '*' $@
;;
fedora*)
v=${distro#fedora}
[ -n "$v" ] && fedora "$v" $@ || fedora '*' $@
;;
debian*)
v=${distro#debian}
[ -n "$v" ] && debian "$v" $@ || debian '*' $@
;;
ubuntu*)
v=${distro#ubuntu}
[ -n "$v" ] && ubuntu "${v:0:2}.${v:2:2}" $@ || ubuntu '*' $@
;;
amazonlinux*)
v=${distro#amazonlinux}
[ -n "$v" ] && amazonlinux "$v" $@ || amazonlinux '*' $@
;;
almalinux*)
v=${distro#almalinux}
[ -n "$v" ] && almalinux "$v" $@ || almalinux '*' $@
;;
rockylinux*)
v=${distro#rockylinux}
[ -n "$v" ] && rockylinux "$v" $@ || rockylinux '*' $@
;;
*)
echo "unknown distro: $1" >&2
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment