Skip to content

Instantly share code, notes, and snippets.

@nelhage
Created September 25, 2014 19:25
Show Gist options
  • Save nelhage/4e1ce736a5d62b2b52e7 to your computer and use it in GitHub Desktop.
Save nelhage/4e1ce736a5d62b2b52e7 to your computer and use it in GitHub Desktop.
import json
import sys
import subprocess
REGIONS = "us-west-1 us-west-2 us-east-1".split()
PROFILES = "default".split()
for region in REGIONS:
for profile in PROFILES:
status = json.loads(subprocess.check_output(
['aws', '--profile', profile, '--region', region, 'ec2', 'describe-instance-status']))
statuses = {}
for s in status['InstanceStatuses']:
if not 'Events' in s:
continue
statuses[s['InstanceId']] = s
all_ids = statuses.keys()
details = json.loads(subprocess.check_output(
['aws', '--profile', profile, '--region', region, 'ec2', 'describe-instances',
'--instance-ids'] + all_ids))
instances = {}
for r in details['Reservations']:
for i in r['Instances']:
instances[i['InstanceId']] = i
for i in instances.values():
name, = [t['Value'] for t in i['Tags'] if t['Key'] == 'Name']
s = statuses[i['InstanceId']]
e, = s['Events']
print "%s %s %s/%s" % (i['InstanceId'], name, e['NotBefore'], e['NotAfter'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment