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.
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)
I saw the answers above and wanted to craft the answers using jq
instead of Bash
or Boto
.
The following examples utilize jq
with AWS cli.
This example solves what the OP was originally trying to do --
List ELB's without instances attached:
aws elb describe-load-balancers --output json |jq -r '.LoadBalancerDescriptions[] | select(.Instances==[]) | . as $l | [$l.LoadBalancerName] | @sh'
Output
'blah10-admin'
'elk-elb-nova'
'cj-web-elb'
This example matches the accepted answer --
Print the ELB name and count of attached instances:
aws elb describe-load-balancers --output json | jq -r '.LoadBalancerDescriptions[] | . as $l | (.Instances | length) as $i | [$l.LoadBalancerName] + [$i] | @sh'
Output
'blah10-admin' 0
'elk-lb-cim-0' 1
'demo-pod-01-es' 1
'elk-elb-nova' 0