Chamara Keragala
Chamara Keragala

Reputation: 5777

Boto3: Get autoscaling group name based on multiple tags

I have a requirement to get the name of the autoscaling group based on its tags.

I have tried following code:

kwargsAsgTags = {
    'Filters': [
        {
            'Name': 'key',
            'Values': ['ApplicationName']
        },
        {
            'Name': 'value',
            'Values': ['my-app-name']
        }
    ]
}

by using above filter I can get the autoscaling group name but since I have same 'ApplicationName' tag used in multiple environments like dev/qa/uat, the output prints all autoscaling groups belong to all environments. How do I filter the EnvironmentName as well?

For that I've tried following but this time it prints all auto-scaling groups belonging to 'dev' environment as well.

kwargsAsgTags = {
    'Filters': [
        {
            'Name': 'key',
            'Values': ['ApplicationName', 'EnvName']
        },
        {
            'Name': 'value',
            'Values': ['my-app-name', 'dev']
        }
    ]
}

Upvotes: 2

Views: 1083

Answers (1)

ELinda
ELinda

Reputation: 2821

You can always get all items and filter in a "brute force" way, or there is a Filter keyword parameter (see boto3 docs)

Excerpt from the docs, on what to put in Name field:

tag:<key> - Accepts the key/value combination of the tag. Use the tag key in the filter name and the tag value as the filter value. The results only include information about the Auto Scaling groups associated with the specified key/value combination.
sess = boto3.Session(profile_name='default')
client = sess.client('autoscaling', config=Config(region_name='us-east-1'))
filters=[
        {
            'Name': 'tag:env',
            'Values': [
                'dev'
            ],
            'Name': 'tag:app',
            'Values': [
                'myapp'
            ]
        },
    ]


asgs = client.describe_auto_scaling_groups(
                        Filters=filters
                    )['AutoScalingGroups']

Upvotes: 0

Related Questions