首页 > 解决方案 > boto3 describe_load_balancers() 什么都不显示,而 aws-cli 显示所有

问题描述

在用 boto3 敲了一阵子之后,我决定做一个健全性检查,以确保我在 AWS 控制台中看到的 elb 和 alb 确实存在。

aws elbv2 describe-load-balancers

aws elb describe-load-balancers

都显示结果。

但是,此代码显示为空(没有错误,但没有结果):

import boto3

def all_lb(lb_type,*args):
    try:
        if lb_type == 'alb':
            elb = boto3.client('elbv2')
        elif lb_type == 'elb':
            elb = boto3.client('elb')
    except Exception as exc:
        print(exc)
        exit(1)

    elb.describe_load_balancers()

print("---- ELB's")
all_lb(lb_type='elb')

print("---- ALB's")
all_lb(lb_type='alb')

我在这里做错了什么?

标签: pythonamazon-web-servicesboto3aws-cli

解决方案


结果在 elb.describe_load_balancers()

import boto3

def all_lb(lb_type,*args):
    try:
        if lb_type == 'alb':
            elb = boto3.client('elbv2')
            name = 'LoadBalancers'
        elif lb_type == 'elb':
            elb = boto3.client('elb')
            name = 'LoadBalancerDescriptions'
    except Exception as exc:
        print(exc)
        exit(1)

    bals = elb.describe_load_balancers()

    for elb2 in bals[name]:
        print(elb2['LoadBalancerName'])

print("---- ELB's")
all_lb(lb_type='elb')

print("---- ALB's")
all_lb(lb_type='alb')

推荐阅读