aws-cli describe-instances, find by date range

Found on Use filter "launch-time" to find all instances newer than X date? using JMESPath query:

aws ec2 describe-instances --query 'Reservations[].Instances[?LaunchTime>=`2015-03-01`][].{id: InstanceId, type: InstanceType, launched: LaunchTime}'

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

Tags:

Aws Cli