Skip to content

Instantly share code, notes, and snippets.

@89465127
Created June 25, 2013 20:09
Show Gist options
  • Save 89465127/5861917 to your computer and use it in GitHub Desktop.
Save 89465127/5861917 to your computer and use it in GitHub Desktop.
Give a summary of currently running/pending AWS instances, and how many are being used out of the limit.
# Setup:
# 1) sudo pip install awscli
# 2) the following environment variables must be set
# - $ export AWS_ACCESS_KEY_ID=<access_key>
# - $ export AWS_SECRET_ACCESS_KEY=<secret_key>
import collections
import datetime
import json
import subprocess
print datetime.datetime.now()
region_names = ['us-west-2', 'us-west-1', 'us-east-1']
key_names = []
for region_name in region_names:
for reservation in json.loads(subprocess.check_output(['aws', '--region', region_name, 'ec2', 'describe-instances', '--filter', 'name=instance-state-code,values=0,16']))['Reservations']:
for instance in reservation['Instances']:
key_names.append(', '.join(group['GroupName'] for group in instance['SecurityGroups']))
instance_counts = collections.Counter(key_names)
running_instances = sum(instance_counts.values())
max_instances = json.loads(subprocess.check_output(['aws', '--region', region_name, 'ec2', 'describe-account-attributes', '--attribute-names', 'max-instances']))['AccountAttributes'][0]['AttributeValues'][0]['AttributeValue']
print "\n{}, using: {}/{}".format(region_name, running_instances, max_instances)
for k, v in instance_counts.iteritems():
print "\t{} : {}".format(k, v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment