0

我有以下 python boto3 代码。让我获得所有带有“SubEnvironment”标签且值为“teal”的 AutoScalingGroup 名称效果很好

我真正想要的是在其中添加值为“foo”的“StackName”,以便我得到我想要的结果。我不熟悉 JMESPath,所以我不知道该怎么做。

def get_autoscale_groups():
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(
            'SubEnvironment', 'teal')
            #Want to add 'StackName', 'foo' somehow
        )

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

    except KeyError:
        break

#print(asg_name_list)
return asg_name_list
4

3 回答 3

2

像这样的东西对我有用:

import boto3

client = boto3.client('autoscaling')
paginator = client.get_paginator('describe_auto_scaling_groups')
page_iterator = paginator.paginate(PaginationConfig={'PageSize': 100})

filtered_asgs = page_iterator.search(
    'AutoScalingGroups[] | [?contains(Tags[?Key==`{}`].Value, `{}`) && contains(Tags[?Key==`{}`].Value, `{}`)]'.format(
        'Purpose', 'loc-save', 'deployment_stage', 'blue')
)

for asg in filtered_asgs:
    print(asg['AutoScalingGroupName'])
于 2018-12-12T20:41:59.627 回答
0

Code:

import boto3
asg_name_list =[]

client = boto3.client('autoscaling')
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, `{}`) && contains(Tags[?Key==`{}`].Value,`{}`)]'.format(
            'SubEnvironment', 'teal','StackName','foo')
        )

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

    except KeyError:
        break

print(asg_name_list)

如果您只需要 aws-cli 命令,请使用以下命令:

aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[?contains(Tags[?Key==`SubEnvironment`].Value,`teal`) && contains(Tags[?Key==`StackName`].Value,`foo`)].[AutoScalingGroupName]' --output text
于 2017-11-02T21:07:36.253 回答
-1

您可以通过传递多个标签来使其动态化,而不是两个标签,如下所示:

import boto3

def get_asg_name(client, tags):
    filter = 'AutoScalingGroups[]'
    paginator = client.get_paginator('describe_auto_scaling_groups')
    page_iterator = paginator.paginate(
        PaginationConfig={'PageSize': 100}
    )
    for tag in tags:
        filter = ('{} | [?contains(Tags[?Key==`{}`].Value, `{}`)]'.format(filter, tag, tags[tag]))
    filtered_asgs = list(page_iterator.search(filter))[-1]#['AutoScalingGroupName']

    return filtered_asgs


if __name__ == '__main__':
    client = boto3.client('autoscaling')
    tags = {'environment': 'test', 'service': 'web'}
    autoscalling_details = get_asg_name(client, tags)
    print(autoscalling_details)
于 2019-03-19T18:32:34.577 回答