samvaran kashyap samvaran kashyap - 4 months ago 44
Python Question

how to limit the ouput of get_all_images in amazon boto?

I tried to load list of AWS images using Python boto library. My code looks like this:

con = boto.connect_ec2(user, pasw)
images_list = con.get_all_images()

How do I get only the first 10 results from the function

I'm looking for something like


As you can find out from the boto documentation you cannot directly limit the amount of images returned by that function. It's lacking the API support (see the link below), it's not exactly a boto's issue.


You can pass a filters parameter though if you want to limit the results based on some specific attributes of those images.

filters (dict) – Optional filters that can be used to limit the results returned. Filters are provided in the form of a dictionary consisting of filter names as the key and filter values as the value. The set of allowable filter names/values is dependent on the request being performed. Check the EC2 API guide for details.

For example

c.get_all_images(filters={'architecture': 'x86_64'})

Supported attributes can be found in the AWS documentation.


The function returns a list so if you really just need to limit the number of results, I'd do this:

images = con.get_all_images()[:10]


Keep in mind that the API does not guarantee any specific order (at least it's not documented), so your first 10 images may differ in time.

If you have too many images, it might be also eating up memory, so I'd really suggest filtering it by some attributes if you can.