Home > OS >  How to get resources from ALL regions using ResourceGroupsTaggingAPI
How to get resources from ALL regions using ResourceGroupsTaggingAPI

Time:11-24

I'm trying to download all ARNs with specific tags with boto3 and ResourceGroupsTaggingAPI. I'm filtering on two tags and get 8 different ARNs when I should get more than 80 ARNs.

**Edit:**I figured out that it's because of a API limitation. According to the documentation, the API allows you to fetch tags from the region you are currently using. Is there any way to fetch tags from ALL regions?

import boto3
import csv


class TagEditor:
    
    def writeToCsv(self,writer, tag_list):
        for resource in tag_list:
            for tag in resource['Tags']:
                row = dict(
                    ResourceArn=resource['ResourceARN'], TagKey=tag['Key'], TagValue=tag['Value'])
                writer.writerow(row)
            
    
    def main(self):
        
        field_names = ['ResourceArn', 'TagKey', 'TagValue']
        s3 = boto3.client('s3')
        restag = boto3.client('resourcegroupstaggingapi')
        
        
        with open('/tmp/tags.csv', 'w') as csvfile:
            writer = csv.DictWriter(csvfile, quoting=csv.QUOTE_ALL,
                                    delimiter=',', dialect='excel', fieldnames=field_names)
            writer.writeheader()
            
            # Filter for tag1
            response_deployment = restag.get_resources(TagFilters=[{'Key':'tag1'}],ResourcesPerPage=100)
            self.writeToCsv(writer, response_deployment['ResourceTagMappingList'])
            
            while 'PaginationToken' in response_deployment and response_deployment['PaginationToken']:
                token = response_deployment['PaginationToken']
                response_deployment = restag.get_resources(TagFilters=[{'Key':'tag1'}], ResourcesPerPage=100, PaginationToken=token)
                self.writeToCsv(writer, response_deployment['ResourceTagMappingList'])
    
            # Filter for tag2
            response_deployment = restag.get_resources(TagFilters=[{'Key':'tag2'}],ResourcesPerPage=100)
            self.writeToCsv(writer, response_deployment['ResourceTagMappingList'])
            
            while 'PaginationToken' in response_deployment and response_deployment['PaginationToken']:
                token = response_deployment['PaginationToken']
                response_deployment = restag.get_resources(TagFilters=[{'Key':'tag2'}], ResourcesPerPage=100, PaginationToken=token)
                self.writeToCsv(writer, response_deployment['ResourceTagMappingList'])
        
        s3.upload_file('/tmp/tags.csv', Bucket = 'my-bucket', Key = 'tags.csv')

CodePudding user response:

That is because you had TagFilters in first call and missed them in subsequent paginated call

Paginated calls must include same set of params including pagination token for subsequent calls

Include TagFilters=[{'Key':'tag1'}] in your get_resources call within loop with pagination token

Iterate over each region and make get_resources call for all regions

# List all regions
ec2_client = boto3.client('ec2')
regions = [region['RegionName'] for region in ec2_client.describe_regions()['Regions']]
  • Related