Skip to content

Instantly share code, notes, and snippets.

@shichao-an
Created October 18, 2014 20:56
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 shichao-an/f0db6196cee4f37bdb84 to your computer and use it in GitHub Desktop.
Save shichao-an/f0db6196cee4f37bdb84 to your computer and use it in GitHub Desktop.
Get EC2 instance type
#!/usr/bin/env python
# Get CPU (core number) and memory by instance_type
# usage: python instance.py (INSANCE_TYPE|VCPU MEMORY)
import json
import sys
import urllib2
JSON_URL = 'https://raw.githubusercontent.com/powdahound/ec2instances.info/master/www/instances.json'
JSON_FILE = urllib2.urlopen(JSON_URL)
STREAM = sys.stdout
def get_metrics(instance_type):
"""
Get metrics by instance type
:param str instance_type: instance type
"""
s = JSON_FILE.read()
j = json.loads(s)
for instance in j:
if instance['instance_type'] == instance_type:
return '%d %.2f' % (instance['vCPU'], instance['memory'])
def get_instance_types(v_cpu, memory):
"""
Get instance type by metrics
:param tuple metrics: a 2-tuple of vCPU and memory
"""
v_cpu = int(v_cpu)
memory = float(memory)
s = JSON_FILE.read()
j = json.loads(s)
res = []
for instance in j:
if instance['vCPU'] == v_cpu and instance['memory'] == memory:
res.append(instance['instance_type'])
return res
if __name__ == '__main__':
if len(sys.argv) >= 2:
if len(sys.argv) == 2:
instance_type = sys.argv[1]
STREAM.write(get_metrics(instance_type) + '\n')
elif len(sys.argv) == 3:
v_cpu = sys.argv[1]
memory = sys.argv[2]
res = get_instance_types(v_cpu, memory)
res_s = '\n'.join(res)
STREAM.write(res_s + '\n')
else:
STREAM.write('No argument given.\n')
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment