Skip to content

Instantly share code, notes, and snippets.

@AvnerCohen
Created June 23, 2022 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AvnerCohen/4fcf47bf242b7b89057622b32a8f01a6 to your computer and use it in GitHub Desktop.
Save AvnerCohen/4fcf47bf242b7b89057622b32a8f01a6 to your computer and use it in GitHub Desktop.
boto3 based script to find ELB (AWS Load balancers) without targets (very näive, use with cautios)
import boto3
LB_TO_TARGET_ARN = {}
def main():
client = boto3.client('elbv2')
load_balancers = client.describe_load_balancers()
for lb in load_balancers['LoadBalancers']:
lb_arn = lb['LoadBalancerArn']
attr_for_lb = client.describe_listeners(LoadBalancerArn=lb_arn)
tg_arn = attr_for_lb['Listeners'][0]['DefaultActions'][0]['TargetGroupArn']
lb_name = lb['LoadBalancerName']
LB_TO_TARGET_ARN[lb_arn] = {'tg_arn': tg_arn, 'lb_name': lb_name}
for item in LB_TO_TARGET_ARN:
tg_arn = LB_TO_TARGET_ARN[item]['tg_arn']
lb_name = LB_TO_TARGET_ARN[item]['lb_name']
tg_instances = client.describe_target_health(TargetGroupArn=tg_arn)
count_targets = len(tg_instances['TargetHealthDescriptions'])
if count_targets == 0:
print(f"Load balancer with no targets for {lb_name}")
else:
pass
#print(f"Total of {count_targets} for {lb_name}")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment