Skip to content

Instantly share code, notes, and snippets.

@awsiv
Created August 27, 2020 16:13
Show Gist options
  • Save awsiv/a94c75f32165408f12d5623bcd49d1c6 to your computer and use it in GitHub Desktop.
Save awsiv/a94c75f32165408f12d5623bcd49d1c6 to your computer and use it in GitHub Desktop.
Find AWS Target Groups without any registered instances
#!/bin/bash
for i in $(aws elbv2 describe-load-balancers --query 'LoadBalancers[*].LoadBalancerArn'| jq -r '.[]'); do
tgCount=$(aws elbv2 describe-target-groups --load-balancer-arn "$i" --query 'TargetGroups[*].TargetGroupArn' | jq -r '.[]'|wc -l)
if [ "$tgCount" -eq 0 ]; then
echo "No TG registered on LB: $i"
else
for t in $(aws elbv2 describe-target-groups --load-balancer-arn "$i" --query 'TargetGroups[*].TargetGroupArn' | jq -r '.[]'); do
instanceCount=$(aws elbv2 describe-target-health --target-group-arn "$t" --query 'TargetHealthDescriptions[*].[Target.Id,TargetHealth.State]' | jq -r '.[]' | wc -l)
if [ "$instanceCount" -eq 0 ]; then
echo "Empty tg: $t"
fi
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment