-
-
Save vancluever/7676b4dafa97826ef0e9 to your computer and use it in GitHub Desktop.
#!/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 |
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
Thanks! bacon saver.
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]"
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!
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
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]"
nice, thanks!
--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.
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
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`
You can also do all of the sorting and filtering in the CLI itself, which should be portable between Windows / Mac / Linux: