Search code examples
amazon-web-servicesamazon-elbaws-cli

AWS cli List unused elb


I am pretty much new to AWS. I was trying to list the load balances which are not attached to any of the instances. I was trying describe-load-balancers using aws cli but was not able to get an option that filters the elbs.

Please provide some suggestions on how to achieve this.


Solution

  • Assuming you have aws cli setup with suitable keys, this long line of shell should list the ELBs with a count of instances attached to them. If it says zero then there are no instances attached

    for i in `aws elb describe-load-balancers|sed -ne 's/"LoadBalancerName": "\(.*\)",/\1/gp'`; do echo -n "$i "; aws elb describe-load-balancers --load-balancer-name $i|grep -c InstanceId;done
    

    Alternatively, here's a boto3 python program

    import boto3
    client=boto3.client('elb')
    bals=client.describe_load_balancers()
    for elb in bals['LoadBalancerDescriptions']:
      count=len(elb['Instances'])
      print "%s %d" % ( elb['LoadBalancerName'], count)