1. List Load Balancers without Listener#

1
2
3
4
5
6
7
8
9
10
11
12
IFS=$'\n'
PROFILE=default
REGION=us-east-1

for lb in $(aws --profile=$PROFILE --region=$REGION elbv2 describe-load-balancers | jq -r '.LoadBalancers[] | "\(.LoadBalancerName) \(.LoadBalancerArn)"'); do
lb_name=$(echo $lb | awk '{print $1}')
lb_arn=$(echo $lb | awk '{print $2}')
lb_listeners_len=$(aws --profile=$PROFILE --region=$REGION elbv2 describe-listeners --load-balancer-arn $lb_arn | jq -r '.Listeners | length')
if [ $lb_listeners_len -eq 0 ]; then
echo "$lb_name"
fi
done

2. List Target Groups without Targets or healthy targets#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
IFS=$'\n'
PROFILE=default
REGION=us-east-1
for tg in $(aws --profile=$PROFILE --region=$REGION elbv2 describe-target-groups | jq -r '.TargetGroups[] | "\(.TargetGroupName) \(.TargetGroupArn) \(.LoadBalancerArns)"'); do
tg_name=$(echo $tg | awk '{print $1}')
tg_arn=$(echo $tg | awk '{print $2}')
tg_lb_arns=$(echo $tg | awk '{print $3}')

if [ $(echo $tg_lb_arns | jq -r '. | length') -eq 0 ]; then
continue
fi

tg_targets=$(aws --profile=$PROFILE --region=$REGION elbv2 describe-target-health --target-group-arn $tg_arn)
tg_targets_len=$(echo $tg_targets | jq -r '.TargetHealthDescriptions | length')
if [ $tg_targets_len -eq 0 ]; then
echo "tg_name: $tg_name"
echo "tg_lb_arns: $tg_lb_arns"
echo "tg_targets_len: $tg_targets_len"
echo "--------------------------------"
fi

echo $tg_targets | jq -r '.TargetHealthDescriptions[] | .TargetHealth.State' | grep -q "healthy"
if [ $? -ne 0 ]; then
echo "tg_name: $tg_name"
echo "tg_lb_arns: $tg_lb_arns"
echo "tg_targets_len: $tg_targets_len"
echo "--------------------------------"
fi
done