Search code examples
amazon-web-servicesboto3aws-sdkaws-resource-group

AWS Get Resources' Tags


I am trying to get different resources' (EC2s, DynamoDB tables, etc) certain tags by querying with resource ID or ARN in lambda functions. Is there a way to achieve this? I've created a resource group and I can see all resources with the tags I want there, but I can't access them from lambda (I don't just want to see the list of resources that belong to the resource group but also each resource's tags' key-value pairs).

I've created a resource group and I can see all resources with the tags I want there, but I can't access them from lambda functions. I want to see resources' tags.


Solution

  • Try the Get Resources API.

    Here's a sample snippet for a Lambda Python environment.
    I've tested it with EC2 and RDS resources, but you can use it for any resource type you'd like.

    import json
    import boto3
        
    client = boto3.client('resourcegroupstaggingapi')
        
    def lambda_handler(event, context):
        response = client.get_resources(
            TagFilters=[
                {
                    'Key': 'tag-key',
                    'Values': [
                       'tag-value',
                    ]
                },
            ]
         )
         return response