amazon-web-servicesaws-clisnapshotamazon-ami

AWS CLI Command to list latest 7 ami' s and there snapshot


I need a cli command to list 7 latest ami's and there snapshot.


Solution

  • Based on Fetch last modified object from AWS S3 CLI, you should be able to limit the result set with:

    aws ec2 describe-images --owners self --query 'sort_by(Images, &CreationDate)[-7:].ImageId' --output text
    

    You should be able to list the associated snapshots with something like this (but I didn't test it):

    aws ec2 describe-images --owners self --query 'sort_by(Images, &CreationDate)[-7:].[ImageId, BlockDeviceMappings.Ebs.SnapshotId]' --output text
    

    That should give you an idea of how to access the fields in the result set. Use the documentation to identify the desired fields.