Skip to content

Instantly share code, notes, and snippets.

@vancluever
Created January 26, 2016 08:05
Show Gist options
  • Star 59 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save vancluever/7676b4dafa97826ef0e9 to your computer and use it in GitHub Desktop.
Save vancluever/7676b4dafa97826ef0e9 to your computer and use it in GitHub Desktop.
Find the most recent Ubuntu AMI using aws-cli (or any other AMI for that matter)
#!/bin/sh
# Use AWS CLI to get the most recent version of an AMI that
# matches certain criteria. Has obvious uses. Made possible via
# --query, --output text, and the fact that RFC3339 datetime
# fields are easily sortable.
export AWS_DEFAULT_REGION=us-east-1
aws ec2 describe-images \
--filters Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64* \
--query 'Images[*].[ImageId,CreationDate]' --output text \
| sort -k2 -r \
| head -n1
@jamesonwilliams
Copy link

Nice!

@arpat
Copy link

arpat commented Sep 28, 2017

Thanks!

@visokoo
Copy link

visokoo commented Oct 2, 2017

You're awesome, thank you!

@blckct
Copy link

blckct commented Mar 28, 2018

Piping it to cut -f 1 will give only the ami name :)

@michaelajr
Copy link

michaelajr commented Apr 27, 2018

Might want to limit that to the Ubuntu Cloud account. Can also use sort_by in the query to get the most recent.

aws ec2 describe-images \
    --owners 099720109477 \
    --filters Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-* \
    --query 'sort_by(Images,&CreationDate)[-1].ImageId'

@ccsalway
Copy link

@michaelajr perfect, thanks.

@aggy07
Copy link

aggy07 commented Jul 11, 2018

Is there any way we can get the Latest AMI for all the regions instead of a specific one?

@sunfriendli
Copy link

Is there a much more canonical filter to use,instead of "Name=name"?In case someone create a ami with similar name.

@xgames330
Copy link

xgames330 commented Oct 24, 2018

Good Find!

Revised to capture all Ubuntu Images:
aws ec2 describe-images --filters "Name=name,Values=ubuntu*" --query "sort_by(Images, &CreationDate)[].Name"

@aggy07 Run command against region that you want, and it will give you the corresponding AMI for that region. Commands must be run against a specific region, no way to list all AMI's in all regions at once.

@vgallissot
Copy link

This is good, thanks!!

@coolblueray
Copy link

With Image Name and AMI ID together,
aws ec2 describe-images --filters "Name=name,Values=ubuntu*" --query "sort_by(Images, &CreationDate)[].[Name, ImageId]"

BTW, Thanks to All of you for posting your comments on this. It helped me!

@nlowe
Copy link

nlowe commented Jun 28, 2019

You can also do all of the sorting and filtering in the CLI itself, which should be portable between Windows / Mac / Linux:

$ aws ec2 describe-images --filters "Name=name,Values=amazon-eks-node-1.11*" --query 'reverse(sort_by(Images, &CreationDate))[0]'
{
    "Architecture": "x86_64",
    "CreationDate": "2019-06-15T06:31:01.000Z",
    "ImageId": "ami-053e2ac42d872cc20",
    "ImageLocation": "amazon/amazon-eks-node-1.11-v20190614",
    "ImageType": "machine",
    "Public": true,
    "OwnerId": "602401143452",
    "State": "available",
    "BlockDeviceMappings": [
        {
            "DeviceName": "/dev/xvda",
            "Ebs": {
                "DeleteOnTermination": true,
                "SnapshotId": "snap-06620aa1d7ab94987",
                "VolumeSize": 20,
                "VolumeType": "gp2",
                "Encrypted": false
            }
        }
    ],
    "Description": "EKS Kubernetes Worker AMI with AmazonLinux2 image (k8s: 1.11.9, docker:18.06)",
    "EnaSupport": true,
    "Hypervisor": "xen",
    "ImageOwnerAlias": "amazon",
    "Name": "amazon-eks-node-1.11-v20190614",
    "RootDeviceName": "/dev/xvda",
    "RootDeviceType": "ebs",
    "SriovNetSupport": "simple",
    "VirtualizationType": "hvm"
}

@RaghavendraP
Copy link

RaghavendraP commented Mar 5, 2020

Is there any way we can get the Latest AMI for all the regions instead of a specific one?

in a bash environment:

for region in `aws ec2 describe-regions --output text | cut -f4`
do
	echo "region: $region, image: ubuntu-xenial-16.04*"
	aws ec2 describe-images --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-xenial-16.04*" \
        --query 'sort_by(Images, &CreationDate)[-1].[CreationDate,Name,ImageId]' --region $region \
        --output text --owners 099720109477
done

@sween
Copy link

sween commented Apr 16, 2020

Thanks! bacon saver.

@emmanuelnk
Copy link

Get the latest IMAGE as seen in the quick start instance screen on the AWS console:

In my case I want the latest ubuntu 20 server arm64 image (Take note of the [-1:] -- return the last element of the array). It will have the image name and ami-id.

aws ec2 describe-images \
--filters "Name=name,Values=ubuntu*20.04-arm64-server*" \
--query "sort_by(Images, &CreationDate)[-1:].[Name, ImageId]"

@themobileprof
Copy link

To get the most recent Bionic 18.04 from Canonical themselves, I had to do

aws ec2 describe-images --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-bionic*" --query "sort_by(Images, &CreationDate)[-1].[Name, ImageId]" --owners 099720109477

Thanks!

@marcelo321
Copy link

I am comparing the output I get from:

aws ec2 describe-images \
--filters "Name=name,Values=ubuntu*20.04-arm64-server*" \
--query "sort_by(Images, &CreationDate)[-1:].[Name, ImageId]"

And the image-id is not the same in the quick-start interface, in the same region

@tiebingzhang
Copy link

This one seems to return the same as the console:

aws ec2 describe-images --filters "Name=name,Values=ubuntu/images/hvm-ssd/*20.04-amd64-server-????????" \
--query "sort_by(Images, &CreationDate)[-1:].[Name, ImageId]"

@rodriguez-matias
Copy link

nice, thanks!

@tdmalone
Copy link

⚠️ A lot of the examples above don't use --owners to limit the account ID to one known to be owned by Canonical. This is a security risk, as anyone can make - and publicly share - an AMI with a similar name, with who-knows-what installed in it.

@stephenlauck
Copy link

To just get "ubuntu jammy" and the latest AMI id only:

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

@zainulabidin302
Copy link

zainulabidin302 commented Sep 17, 2023

Only image id with same original command:

AMI_ID=`aws ec2 describe-images \
 --filters 'Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-*' \
 --query 'Images[*].[ImageId,CreationDate]' --output text \
 | sort -k1 -r \
 | head -n1 | cut -f 1 -w`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment