Create a gist now

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Show comment
Hide comment
@jamesonwilliams

jamesonwilliams commented Mar 19, 2017

Nice!

@arpat

This comment has been minimized.

Show comment
Hide comment
@arpat

arpat commented Sep 28, 2017

Thanks!

@visokoo

This comment has been minimized.

Show comment
Hide comment
@visokoo

visokoo Oct 2, 2017

You're awesome, thank you!

visokoo commented Oct 2, 2017

You're awesome, thank you!

@blckct

This comment has been minimized.

Show comment
Hide comment
@blckct

blckct Mar 28, 2018

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

blckct commented Mar 28, 2018

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

@michaelajr

This comment has been minimized.

Show comment
Hide comment
@michaelajr

michaelajr 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'

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

This comment has been minimized.

Show comment
Hide comment
@ccsalway

ccsalway Apr 28, 2018

@michaelajr perfect, thanks.

ccsalway commented Apr 28, 2018

@michaelajr perfect, thanks.

@aggy07

This comment has been minimized.

Show comment
Hide comment
@aggy07

aggy07 Jul 11, 2018

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

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

This comment has been minimized.

Show comment
Hide comment
@sunfriendli

sunfriendli Jul 26, 2018

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

sunfriendli commented Jul 26, 2018

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

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