In boto3, where to put "if" statement to output only values with an empty array?

310 views Asked by At

I am working with aws boto3, still very new to coding & have to find how to drill down my output to only return load balancers/target groups with no target connections.

  def gettargets(arn):
    tglist=elb.describe_target_health(TargetGroupArn=arn)
    targets=[]
    for targetid in tglist["TargetHealthDescriptions"]:
        targets.append(targetid['Target']['Id']) 
    print("Targets:",targets)

elbs = elb.describe_load_balancers(PageSize=100)
for loadbalancer in elbs["LoadBalancers"]:
    print("\n"*2)
    print("-"*8)
    print("ELB Name:",loadbalancer["LoadBalancerName"])
    print("Type:",loadbalancer["Type"])
    print("Scheme:",loadbalancer["Scheme"])
    print("TargetGroups:",str(gettargetgroups(loadbalancer["LoadBalancerArn"])))
    for tgs in gettargetgrouparns(loadbalancer["LoadBalancerArn"]):
     gettargets(tgs)

The Output looks like

    --------
ELB Name: example-elb
Type: application
Scheme: internet-facing
TargetGroups: ['example-tg']
Targets: ['i-09876543210']
    --------
ELB Name: example-alb-2
Type: application
Scheme: internal
TargetGroups: ['example-tg-2']
Targets: []

I need help returning the sections with empty Targets: []

Should I put an if statement in the getargets function or make an if statement for at the bottom. I can sort of read python, and have yet to be able to conceptualize structure and how the pieces work.

2

There are 2 answers

1
shomeax On BEST ANSWER

You're don't actually 'return' anything here, you just dump results to the screen immediately as you get them.

Split logic and printing and there you have it.

def gettargets(arn):
    tglist=elb.describe_target_health(TargetGroupArn=arn)
    targets=[]
    for targetid in tglist["TargetHealthDescriptions"]:
        targets.append(targetid['Target']['Id']) 
    # now we return, not print!
    return targets



elbs = elb.describe_load_balancers(PageSize=100)
for loadbalancer in elbs["LoadBalancers"]:
    
    targets = []
    for tgs in gettargetgrouparns(loadbalancer["LoadBalancerArn"]):
        targets += gettargets(tgs)

    # print the whole stuff only if targets is empty
    if targets == []:
        print("\n"*2)
        print("-"*8)
        print("ELB Name:",loadbalancer["LoadBalancerName"])
        print("Type:",loadbalancer["Type"])
        print("Scheme:",loadbalancer["Scheme"])
        print("TargetGroups:", str(gettargetgroups( loadbalancer["LoadBalancerArn"])))
        print("Targets:",targets)

1
Marcin On

You can just return targets from gettargets, and then check if its empty:

  def gettargets(arn):
    tglist=elb.describe_target_health(TargetGroupArn=arn)
    targets=[]
    for targetid in tglist["TargetHealthDescriptions"]:
        targets.append(targetid['Target']['Id']) 
    return targets
    

elbs = elb.describe_load_balancers(PageSize=100)
for loadbalancer in elbs["LoadBalancers"]:
    targets = []
    
    for tgs in gettargetgrouparns(loadbalancer["LoadBalancerArn"]):
       targets += gettargets(tgs)

    if not targets:
        print("\n"*2)
        print("-"*8)
        print("ELB Name:",loadbalancer["LoadBalancerName"])
        print("Type:",loadbalancer["Type"])
        print("Scheme:",loadbalancer["Scheme"])
        print("TargetGroups:",str(gettargetgroups(loadbalancer["LoadBalancerArn"])))
        print("Targets:",  targets)