From 5342f3c5955e3cae323c801f633c6ced38b15316 Mon Sep 17 00:00:00 2001 From: x p k Date: Tue, 23 Apr 2024 23:13:06 +0800 Subject: [PATCH] FIX: added pagination looping support to get all resources for aws-inventory-from-awsconfig.py --- aws/aws-inventory-from-awsconfig.py | 35 ++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/aws/aws-inventory-from-awsconfig.py b/aws/aws-inventory-from-awsconfig.py index c45792e..ac763fc 100755 --- a/aws/aws-inventory-from-awsconfig.py +++ b/aws/aws-inventory-from-awsconfig.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 - +""" +TODO: +get_discovered_resource_counts has a limit of 100 resources. need to use nextToken +to get count of all resources +""" import boto3 import pandas as pd @@ -7,17 +11,28 @@ import csv client = boto3.client('config') resp = client.get_discovered_resource_counts() +# print('totalDiscoveredResources', resp['totalDiscoveredResources'], sep=": ") results = [] -# print("ResourceType, ResourceId") for item in resp['resourceCounts']: - if item['resourceType'] == "AWS::Config::ResourceCompliance": - continue reslist = client.list_discovered_resources(resourceType=item['resourceType']) - - for res in reslist['resourceIdentifiers']: - # print(item['resourceType'], res['resourceId'], sep=', ') - results += [[item['resourceType'], res['resourceId']]] + nextToken = reslist.get('nextToken') + while True: + for res in reslist['resourceIdentifiers']: + if item['resourceType'] in [ + "AWS::Config::ResourceCompliance", + "AWS::Backup::RecoveryPoint", + "AWS::RDS::DBSnapshot", + "AWS::SSM::ManagedInstanceInventory", + "AWS::SSM::AssociationCompliance", + "AWS::IAM::Policy" + ]: + continue + results += [[item['resourceType'], res['resourceId']]] + if nextToken is None: + break + reslist = client.list_discovered_resources(resourceType=item['resourceType'], nextToken=nextToken) + nextToken = reslist.get('nextToken') df = pd.DataFrame(results, columns=['ResourceType', 'ResourceId']) -print(df.to_csv(index=False,quoting=csv.QUOTE_NONNUMERIC)) -# print(results) \ No newline at end of file +# print(df) +print(df.to_csv(index=False, quoting=csv.QUOTE_NONNUMERIC))