How to list down Amazon EC2 AMI using your terminal?

Published: at 11:11 AM

When managing cloud resources, it is important to consider the costs associated with AWS EC2 instances, which can add up quickly, especially when using high-configuration instances unnecessarily. For testing purposes, it is wise to choose low-configuration machines such as the t4g.nano instance. These instances are designed to offer a cost-effective solution for workloads that do not need significant computational power, enabling developers and testers to carry out their activities efficiently without incurring excessive expenses.

While it is cost-effective, finding the right instances can be tricky sometimes. I came across a scenario where I needed an older version of an OS that should also be Arm 64 architecture. Using the AWS console, it is easy to get the latest OS AMIs, but finding older ones can be tricky.

You’ve come to the right place if you are stuck in a similar problem.

Let’s get Started 🚀

The first step is configuring your terminal with your cloud environment’s credentials. Follow this article to configure your terminal.

To ensure your terminal is properly connected, try running a simple AWS CLI command like aws s3 ls. This CLI command would list the buckets in your cloud environment.

In this article, I am searching for AMIs in the ap-south-1 (Mumbai) region.

aws ec2 describe-images --region ap-south-1 --owners amazon --query 'Images[*].[ImageId,Name,Architecture]' --output table

This command will generate a list of AMIs in the Mumbai region.

List of AMIs

Now, you can simply select your EC2 AMI and spin up an EC2 instance using the AWS EC2 run-instances.

Narrowing the list down

The command above works, but describe-images --owners amazon returns thousands of rows. It is slow, and scrolling a table that long defeats the purpose. Filters are what make this practical.

Filter by name pattern and architecture — this is the Arm 64 Ubuntu case I started with:

aws ec2 describe-images \
  --region ap-south-1 \
  --owners 099720109477 \
  --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-arm64-server-*" \
            "Name=state,Values=available" \
  --query 'Images[*].[ImageId,Name,CreationDate]' \
  --output table

Two things to note:

Getting the newest AMI (or a deliberately old one)

describe-images returns results in no particular order, so “the latest” needs an explicit sort. JMESPath can do it inside the query:

aws ec2 describe-images \
  --region ap-south-1 \
  --owners 099720109477 \
  --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-arm64-server-*" \
  --query 'sort_by(Images, &CreationDate)[-1].ImageId' \
  --output text

That prints a bare AMI ID — ready to drop straight into a script:

AMI_ID=$(aws ec2 describe-images --region ap-south-1 --owners 099720109477 \
  --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-arm64-server-*" \
  --query 'sort_by(Images, &CreationDate)[-1].ImageId' --output text)

aws ec2 run-instances --region ap-south-1 \
  --image-id "$AMI_ID" --instance-type t4g.nano --key-name my-key

For an older build — my original problem — swap [-1] for a slice and read the dates:

--query 'sort_by(Images, &CreationDate)[-10:].[ImageId,Name,CreationDate]'

That gives the last ten builds newest-last, which is usually enough to spot the release you actually want.

The shortcut: SSM Parameter Store

If you only ever need the current image, AWS publishes the latest AMI IDs as public SSM parameters, and looking one up is a single fast call instead of a filtered scan:

aws ssm get-parameter \
  --region ap-south-1 \
  --name /aws/service/canonical/ubuntu/server/22.04/stable/current/arm64/hvm/ebs-gp2/ami-id \
  --query 'Parameter.Value' --output text

Amazon Linux has an equivalent under /aws/service/ami-amazon-linux-latest/. This is the right choice for automation and CloudFormation. It is not useful for finding old AMIs, which is exactly why describe-images is still worth knowing.

Gotchas worth knowing

And that’s it!

ko-fi