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

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
| improve this answer | |
  • holy, cow, that is nearly exactly the solution I settled on. "use boto, and do the filtering myself" – JDS Mar 10 '15 at 17:32

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}'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.