使用boto3指定AWS ELB的所有区域

时间:2018-10-30 19:46:52

标签: python-3.x amazon-web-services aws-sdk boto3

我正在尝试通过以下代码检查所有区域是否有ELB资源:

elb = boto3.client("elbv2")

x = elb.describe_load_balancers()
x = x["LoadBalancers"]

print(x)

n=0
for entity in x:
    for k, v  in entity.items():
        if k == "LoadBalancerName":
            n+=1

print(n)

这仅签入已配置的默认区域。我可以传递一个参数来指定python中的所有区域吗?

1 个答案:

答案 0 :(得分:0)

您需要遍历每个区域,然后执行检查。像这样:

# Get a list of Regions
ec2 = boto3.client('ec2')
region_list = [region['RegionName'] for region in ec2.describe_regions()['Regions']]

# Loop through each region
for region in region_list:

  # Connect to ELB in given region
  elb = boto3.client("elbv2", region_name=region)
  x = elb.describe_load_balancers()
  ...