代码之家  ›  专栏  ›  技术社区  ›  Alex

为什么从python脚本和awscli接收到的启动配置数量不同?

  •  1
  • Alex  · 技术社区  · 6 年前

    返回启动配置列表的python脚本如下(对于us-east-1区域):

    autoscaling_connection = boto.ec2.autoscale.connect_to_region(region)
    nlist = autoscaling_connection.get_all_launch_configurations()
    

    由于某些原因,nlist的长度是50,也就是说,我们只找到了50个发射配置。AWS CLI中的相同查询将产生174个结果:

    aws autoscaling describe-launch-configurations --region us-east-1 | grep LaunchConfigurationName | wc
    

    为什么偏差这么大?

    1 回复  |  直到 6 年前
        1
  •  2
  •   MLu    6 年前

    因为 get_all_launch_configurations boto2 的函数,但是一个类似的函数 describe_launch_configurations boto3 提到:

    https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/autoscaling.html#AutoScaling.Client.describe_launch_configurations

    参数

    最大记录 (整数)--返回此值的最大项数 打电话。默认值为50,最大值为100。

    下一个 返回。(您从上一次通话中收到此令牌。)

    get_all_launch_configurations() 名下 max_records next_token here

    先打电话给 NextToken="" 您将获得前50个(或最多100个)启动配置。在返回的数据中查找 NextToken 下一个 .

    data = conn.get_all_launch_configurations()
    process_lc(data['LaunchConfigurations'])
    while 'NextToken' in data:
        data = conn.get_all_launch_configurations(next_token=data['NextToken'])
        process_lc(data['LaunchConfigurations'])
    

    希望有帮助:)

    博托3

    更新-boto2与boto3:

    看起来像 博托2 不会再回来了 下一个 ,它更好,更符合逻辑,真的:)

    下面是一个实际的脚本:

    #!/usr/bin/env python3
    
    import boto3
    
    def process_lcs(launch_configs):
        for lc in launch_configs:
            print(lc['LaunchConfigurationARN'])
    
    client = boto3.client('autoscaling')
    
    response = client.describe_launch_configurations(MaxRecords=1)
    process_lcs(response['LaunchConfigurations'])
    
    while 'NextToken' in response:
        response = client.describe_launch_configurations(MaxRecords=1, NextToken=response['NextToken'])
        process_lcs(response['LaunchConfigurations'])
    

    我故意设置 MaxRecords=1