Skip to content

Instantly share code, notes, and snippets.

@gleicon
Created April 5, 2019 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gleicon/a381a5cc4bcaf64d207cc64f10c5ac17 to your computer and use it in GitHub Desktop.
Save gleicon/a381a5cc4bcaf64d207cc64f10c5ac17 to your computer and use it in GitHub Desktop.
cross check reservation IDs against running instances on aws
import boto3
import pprint
from collections import defaultdict
ec2 = boto3.client('ec2')
resp = ec2.describe_instances()
running_instances = defaultdict (lambda:0)
running_instances_apps = defaultdict(set)
for reservation in resp['Reservations']:
for instance in reservation['Instances']:
state = instance['State']['Name']
if state == 'running':
instance_type = instance['InstanceType']
tags = instance['Tags']
name = [tag['Value'] for tag in tags if tag['Key'] == 'Name']
running_instances[instance_type] +=1
running_instances_apps[instance_type].add(",".join(name))
#print instance['InstanceId'], instance['LaunchTime'], instance_type, state, name
resp = ec2.describe_reserved_instances()
print("reservation_id,model,reserved count,start date, end date, running count, apps using these instance")
# load reserved instance counts per model
for reservation in resp['ReservedInstances']:
if reservation['State'] == 'active':
instance_type = reservation['InstanceType']
reservation_id = reservation['ReservedInstancesId']
print "{},{},{},{},{},{},{}".format(reservation_id,instance_type, reservation['InstanceCount'], reservation['Start'], reservation['End'], running_instances[instance_type], "|".join(running_instances_apps[instance_type]))
# {u'ReservedInstancesId': '4357912c-f422-499a-8dcd-0a9b76828366', u'OfferingType': 'Heavy Utilization', u'AvailabilityZone': 'us-east-1b', u'End': datetime.datetime(2015, 2, 13, 23, 59, 59, tzinfo=tzutc()), u'ProductDescription': 'Windows', u'Scope': 'Availability Zone', u'UsagePrice': 0.0, u'RecurringCharges': [{u'Amount': 0.145, u'Frequency': 'Hourly'}], u'OfferingClass': 'standard', u'Start': datetime.datetime(2014, 2, 14, 0, 0, tzinfo=tzutc()), u'State': 'retired', u'FixedPrice': 780.0, u'CurrencyCode': 'USD', u'Duration': 31536000, u'InstanceTenancy': 'default', u'InstanceType': 'm1.large', u'InstanceCount': 1}
# for instance in reservation['Instances']:
# state = instance['State']['Name']
# if state == 'running':
# tags = instance['Tags']
# name = [tag['Value'] for tag in tags if tag['Key'] == 'Name']
# print instance['InstanceId'], instance['LaunchTime'], instance['InstanceType'], state, name
#if instance['State']['Name'] is "running":
# pprint.pprint(instance)
# print instance["InstanceId"]
# list and count all instances marking how many fit under a reservation tag
@gleicon
Copy link
Author

gleicon commented Apr 5, 2019

caveat - running instances will be repeated at each reservation id line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment