0

I have the following python boto3 code. It works fine to get me all the AutoScalingGroup names with a tag of "SubEnvironment" that has a value of "teal"

What I really want is to add "StackName" with a value of "foo" to this so that I get exactly the results I want. I'm unfamiliar with JMESPath so I'm not sure how to do this.

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
jb62
  • 1,400
  • 1
  • 10
  • 19
  • This should help: http://jmespath.org/specification.html?highlight=boolean#and-expressions. Suspect that you can write: ?contains(Tags[?Key==`a`].Value, `b`) && contains(Tags[?Key==`c`].Value, `d`) – jarmod Nov 02 '17 at 19:10

3 Answers3

2

Something like this worked for me:

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'])
sprut
  • 51
  • 3
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)

If you want just the aws-cli command, then use the one below:

aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[?contains(Tags[?Key==`SubEnvironment`].Value,`teal`) && contains(Tags[?Key==`StackName`].Value,`foo`)].[AutoScalingGroupName]' --output text
Madhukar Mohanraju
  • 2,341
  • 8
  • 28
0

Instead of two tags, you can make it dynamic by passing the multiple tags like this:

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)
Arbab Nazar
  • 18,514
  • 8
  • 66
  • 74