Skip to content

Instantly share code, notes, and snippets.

@kryptek
Created December 11, 2013 17:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kryptek/7914531 to your computer and use it in GitHub Desktop.
Save kryptek/7914531 to your computer and use it in GitHub Desktop.
Retrieve an EC2 Load Balancers instances and their health status using python
import boto
import sys
ec2 = boto.connect_ec2()
elb = boto.connect_elb()
load_balancer = elb.get_all_load_balancers(load_balancer_names=[sys.argv[1]])[0]
health = load_balancer.get_instance_health()
instances = ec2.get_only_instances(instance_ids=[instance.id for instance in load_balancer.instances])
headers = ['Instance ID', 'Address', 'Flavor', 'Status']
row_format = "{:>15}" * (len(headers) + 1)
print row_format.format("",*headers)
for instance in instances:
print row_format.format('',*[instance.id, instance.ip_address, instance.instance_type, [str(i.state) for i in health if i.instance_id == instance.id ][0]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment