-1

I'm using boto3 API to describe_image from AWS. My list AMI has the same name, AWS automatically generate suffix on that name. I wonder is there any option to describe_image in creation time order list. Currently I have to sort programmatically on that return dict.

Any help would be appreciated.

2 Answers2

2

No. There is no capability to request the data back in a particular order.

You can use a Filter to limit the results, but not to sort the results.

You would need to programmatically sort the results to identify your desired AMI.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • I did programmatically sort the results but I just wonder is there any option from AWS to do that. I can limit the the results but it will not contain the needed results. Thank you for confirming. – hoài vinh nguyễn Nov 07 '18 at 09:25
1

For reference, sorting Images by creation date can be done as follows

import boto3

def image_sort(elem):
    return elem.get('CreationDate')

ec2_client = boto3.client('ec2')
images = ec2_client.describe_images(ImageIds=["my", "ami", "ids"])

# The ec2 describe_images call returns a dict with a key of 
# 'Images' that holds a list of image definitions.
images = images.get('Images')

# After getting the images, we can use python's list.sort 
# method, and pass a simple function that gets the item to sort on.
images.sort(key=image_sort)

print(images)
Garrett F.
  • 76
  • 5