【问题标题】:Paginate and list out auto scaling group names with a specific application tag using boto3使用 boto3 对具有特定应用程序标签的 Auto Scaling 组名称进行分页并列出
【发布时间】:2018-04-13 02:51:35
【问题描述】:

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

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

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

  2. 如果对 #1 的回答是否定的 - 有人可以帮助我了解如何对所有 asg 进行分页,直到所有内容都检查了相关标签?

谢谢...

【问题讨论】:

    标签: boto3 autoscaling


    【解决方案1】:
        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'])
    

    【讨论】:

      【解决方案2】:
      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)
      

      【讨论】:

      • 这似乎有效。 try/except 块是必要的,否则您无法摆脱“while true”。总而言之,感觉有点笨拙,但我不确定更好的方法。我很乐意就更优雅的方式获得一些建议。我试图通过打印访问filtered_asgs,但它只是在内存中提供了一个位置,所以考虑到我在截止日期前,我把asg_name_list 扔进去了。当然有更好的方法...
      猜你喜欢
      • 2017-09-22
      • 2016-01-17
      • 2018-09-30
      • 2017-07-31
      • 2018-07-27
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      相关资源
      最近更新 更多