Skip to content

Instantly share code, notes, and snippets.

@kd7lxl
Created October 25, 2018 15:56
Show Gist options
  • Save kd7lxl/2201196a65bea538dc773212f9e47b3f to your computer and use it in GitHub Desktop.
Save kd7lxl/2201196a65bea538dc773212f9e47b3f to your computer and use it in GitHub Desktop.
Lists AWS ELBs with no valid endpoints
import boto.ec2.elb
def get_elbs_with_no_valid_instances(elb):
"""Finds ELBs with zero instances or where all instances are no longer in service."""
for lb in elb.get_all_load_balancers():
instances = lb.get_instance_health()
if len(instances) == 0:
yield lb
continue
if all([instanceState.state == 'OutOfService' for instanceState in instances]):
yield lb
continue
def main():
for region in boto.ec2.elb.regions():
elb = boto.ec2.elb.connect_to_region(region.name)
try:
elbs = list(get_elbs_with_no_valid_instances(elb))
except boto.exception.BotoServerError as err:
continue
if not elbs:
continue
print "%s:" % region.name
for lb in sorted(elbs, key=lambda lb: lb.name.lower()):
print "\t%s" % lb.name
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment