如何使用 ResourceGroupsTaggingAPI 从所有区域获取资源
How to get resources from ALL regions using ResourceGroupsTaggingAPI
我正在尝试使用 boto3 和 ResourceGroupsTaggingAPI 下载所有带有特定标签的 ARN。我在两个标签上过滤并得到 8 个不同的 ARN,而我应该得到超过 80 个 ARN。
**编辑:**我发现这是因为 API 限制。根据文档,API 允许您从当前使用的区域获取标签。有没有办法从所有地区获取标签?
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')
那是因为您在第一次调用时使用了 TagFilters,但在随后的分页调用中错过了它们
分页调用必须包含相同的参数集,包括用于后续调用的分页标记
在您的 get_resources 循环调用中包含 TagFilters=[{'Key':'tag1'}]
和分页标记
遍历每个区域并对所有区域进行 get_resources 调用
# List all regions
ec2_client = boto3.client('ec2')
regions = [region['RegionName'] for region in ec2_client.describe_regions()['Regions']]
我正在尝试使用 boto3 和 ResourceGroupsTaggingAPI 下载所有带有特定标签的 ARN。我在两个标签上过滤并得到 8 个不同的 ARN,而我应该得到超过 80 个 ARN。
**编辑:**我发现这是因为 API 限制。根据文档,API 允许您从当前使用的区域获取标签。有没有办法从所有地区获取标签?
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')
那是因为您在第一次调用时使用了 TagFilters,但在随后的分页调用中错过了它们
分页调用必须包含相同的参数集,包括用于后续调用的分页标记
在您的 get_resources 循环调用中包含 TagFilters=[{'Key':'tag1'}]
和分页标记
遍历每个区域并对所有区域进行 get_resources 调用
# List all regions
ec2_client = boto3.client('ec2')
regions = [region['RegionName'] for region in ec2_client.describe_regions()['Regions']]