Finding Unused Elastic Load Balancers

A Load Balancer
A Load Balancer

AWS imposes limits on the number of Elastic LoadBalancers. Before asking for a limit increase, it is worthwhile to check if your load balancers are actually used and have healthy instances.

Using excellent boto framework for Python, I built a simple script to find all ELBs where there is an instance in OutOfService state or where there are no instances at all:

from optparse import OptionParser
from time import sleep
import boto.ec2.elb

itembuffer = []

parser = OptionParser()

parser.add_option("-i", "--awsKeyId", 
    dest="awsKeyId", type="string", action="store", 
    help="AWS Access Key Id")
parser.add_option("-k", "--awsSecretKey", 
    dest="awsSecretKey", type="string", action="store", 
    help="AWS Secret Access Key")

(options, args) = parser.parse_args()


elb = boto.ec2.elb.connect_to_region('us-east-1' ,
    aws_access_key_id=options.awsKeyId,
    aws_secret_access_key=options.awsSecretKey)


allElbs = elb.get_all_load_balancers()
for lb in allElbs:
    instances = lb.get_instance_health()
    if len(instances)==0:
        print lb
    for instanceState in instances:
        if  instanceState.state == 'OutOfService':
            print lb

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s