AWS boto Get snapshots over time

I am using AWS and pulling snapshots using boto ("Python Web Services Python Interface"). I am fetching all the pictures using conn.get_all_snapshots()

, but I only want to get the data I need. I use a calendar to view the snapshots, so it would be very helpful if I could only pull out snapshots for the current month that I am viewing.

Is there a limitation (filter maybe) I can conn.get_all_snapshots()

only wear to get snapshots for a month?

Boto docs as needed: http://boto.readthedocs.org/en/latest/ref/ec2.html

0


source to share


2 answers


I do not know how to do that. The EC2 API allows you to filter results based on snapshot ID or with various filters such as status

or progress

. There is even a filter for create-time

, but unfortunately there is no way to specify a range of times and return it in between. And there is no way to use a filter <

or in a query >

.



0


source


Use a snapshot field start_time

(which is a string, so it needs to be parsed):

import datetime

# Fetch all snaps
snaps = conn.get_all_snapshots()
# Get UTC of 30-days ago
cutoff = datetime.datetime.utcnow() - datetime.timedelta(days=30)
# datetime parsing format "2015-09-07T20:12:08.000Z"
DATEFORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
# filter older
old_snaps = [s for s in snaps \
             if datetime.datetime.strptime(s.start_time, DATEFORMAT) < cutoff]
# filter newer
new_snaps = [s for s in snaps \
             if datetime.datetime.strptime(s.start_time, DATEFORMAT) >= cutoff]

      



old_snaps

will contain those from before this month and new_snaps

will contain those from this month. (I have a feeling you want to delete old snapshots, so I included the line old_snaps

.)

I am using datetime.strptime () above because it is built in, but dateutil is more reliable if you have it installed. (See this for details: fooobar.com/questions/25533 / ... )

0


source







All Articles