Aws-cli describe-instances, find by date range

aws-cli

Using the aws-cli client (https://github.com/aws/aws-cli), is there a way to find instances using a date range filter? Or using an "earlier than X date" or "last X days" filter?

It seems that the only date-related filter is to specify an exact date, or a partial date with string wildcards. For example, I've found that specifying a date as follows works:

aws ec2 describe-instances --filters "Name=launch-time,Values=2015-03\*"

For example, that gets all instances launched in March, 2015.

What I want is equivalent to this POSIX "find" command, "find all things from last 30 days":

find . -mtime -30

Best Answer

You cant, but to do it in python using the boto library do like this, for example, to list the instances in aws region "eu-west-1" launched more than 30 days ago.

import boto.ec2
import datetime
from dateutil import parser
conn = boto.ec2.connect_to_region('eu-west-1')
reservations = conn.get_all_instances()
for r in reservations:
    for i in r.instances:
        launchtime = parser.parse(i.launch_time)
        launchtime_naive = launchtime.replace(tzinfo=None)
        then = datetime.datetime.utcnow() + datetime.timedelta(days = -30)
        if launchtime_naive < then:
            print i.id