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