使用boto3

时间:2017-10-31 20:14:12

标签: boto3 autoscaling

我已经在这里看到了建议的答案: List out auto scaling group names with a specific application tag using boto3

但是,它只处理前100个结果。它没有演示如何处理所有“页面”。我在大型AWS环境中工作,有两个问题。

  1. 有没有办法要求用一定的字符串标记asg,而不是解析环境中的所有内容?

  2. 如果对#1的答案是否定的 - 有人可以帮助我了解如何对所有asg进行分页,直到检查完所有标签为止?

  3. ...谢谢

2 个答案:

答案 0 :(得分:0)

def get_autoscale_groups(region):

   client = boto3.client('autoscaling', region_name=region)

   asg_name_list = []

   while True:
       paginator = client.get_paginator('describe_auto_scaling_groups')
       page_iterator = paginator.paginate(
           PaginationConfig={'PageSize': 100}
       )
       for page in page_iterator:
           filtered_asgs = page_iterator.search(
               'AutoScalingGroups[] | [?contains(Tags[?Key==`{}`].Value, `{}`)]'.format(
            'SomeId', 'blah')
        )

           for asg in filtered_asgs:
               asg_name_list.append(asg['AutoScalingGroupName'])
       try:
           marker = page['Marker']
           print(marker)

       except KeyError:
           break

   print(asg_name_list)

答案 1 :(得分:0)

    asg_client = session.client('autoscaling')
    paginator = asg_client.get_paginator('describe_auto_scaling_groups')
    asg_list = paginator.paginate()

    for x in asg_list:
        for asg in x['AutoScalingGroups']:
            for tag in asg['Tags']:
                if tag['Key'] == 'Solution':
                    print('ASG Name: '+ asg['AutoScalingGroupName'] + ' Tag: ' + tag['Value'])