Getting the latest AMIs with AWS CLI can get complicated if you haven’t found a personal preference to re-utilize, since there are several ways to search and get a list of AMIs. Here I will show you mine, which I have found out to be the fastest for me.
First, let’s learn how we structure AMIs names:
ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-20220420
ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-arm64-server-20220420
ubuntu-minimal/images/hvm-ssd/ubuntu-jammy-22.04-amd64-minimal-20220420
ubuntu-pro-server/images/hvm-ssd/ubuntu-jammy-22.04-amd64-pro-server-20230420
ubuntu-pro-fips-server/images/hvm-ssd/ubuntu-focal-20.04-amd64-pro-fips-server
ubuntu-eks/k8s_1.21/images/hvm-ssd/ubuntu-focal-20.04-arm64-server-20220506
If you read carefully the pattern, you will see that there is a defined structure that goes as follows:
${FAMILY}/${RELEASE_STREAM}/${VIRT}-${STORAGE}/ubuntu-${SUITE}-${VERSION}-${ARCH}-server-${SERIAL}
FAMILY = ubuntu || ubuntu-minimal || ubuntu-pro-server || ubuntu-eks
RELEASE_STREAM = images (aka release) || images-testing (aka daily)
VIRT = hvm (there are no others anymore. pv was formerly available)
STORAGE = ssd || instance (instance only available on 18.04, and deprecated by AWS)
SUITE = lunar, kinetic, jammy, focal, bionic, xenial (written name of Ubuntu version)
VERSION = 23.04, 22.10, 22.04, 20.04, 18.04, 16.04 (numeric name of Ubuntu version
ARCH = amd64 || arm64
SERIAL = date of creation. This is a constantly moving value
Note:
For EKS images, the structure is similar but the product definition includes one extra level for the K8s version, e.g. ubuntu-eks/k8s_1.25/...
Note 2:
EKS images are currently also being published on AWS Marketplace, so searching ubuntu-eks
images produce both results. The ones listed on marketplace are owned by AWS, with 679593333241
as Owner ID. They are the same except that the Marketplace AMIs need a subscription first before launch.
So, if you want to get the free version of Focal (20.04) for ARM, the AMI name would be:
ubuntu/images/hvm-ssd/ubuntu-focal-20.04-arm64-server-*
The corresponding version for Ubuntu Pro:
ubuntu-pro-server/images/hvm-ssd/ubuntu-focal-20.04-arm64-pro*
Note:
Always check for the AMI owner (publisher), as you may end up with an unsupported 3rd party AMI.
Official Ubuntu images on AWS are either published under AWS’ OwnerID 679593333241 or Canonical’s id 099720109477
Having that in mind, now we can search using the command: aws cli describe-images
. Let’s search for Ubuntu Jammy 22.04 LTS:
aws ec2 describe-images --output json --region us-east-1 \
--filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server*"
But this will give you a long list of all the available releases for this version, so let’s add a way to get only the latest (Canonical releases periodically the latest images, so always search for the latest AMI)
We will use the built-in query mechanism, which also includes a sorting function.
The search for Ubuntu 22.04 LTS would be like this:
aws ec2 describe-images --output json --region us-east-1 \
--filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server*" \
--query 'sort_by(Images, &CreationDate)[-1].{Name: Name, ImageId: ImageId, CreationDate: CreationDate, Owner:OwnerId}'
If we want to get the Pro version:
aws ec2 describe-images --output json --region us-east-1 \
--filters "Name=name,Values=ubuntu-pro-server/images/hvm-ssd/ubuntu-jammy-22.04-amd64-pro*" \
--query 'sort_by(Images, &CreationDate)[-1].{Name: Name, ImageId: ImageId, CreationDate: CreationDate, Owner:OwnerId}'
You will get a single json object with the latest AMI published.
{
"Name": "ubuntu-pro-server/images/hvm-ssd/ubuntu-jammy-22.04-amd64-pro-server-20230420",
"ImageId": "ami-028f6d7ddc3079baf",
"CreationDate": "2023-04-20T02:32:06.000Z",
"Owner": "099720109477"
}
Note:
AMI IDs are unique per region. So if you need to use another region, you will get different AMI IDs. This is important to remember when automating pipelines, as you may get a lot of “AMI not found” errors.