code-dumps/aws/aws-inventory-from-awsconfig.py

33 lines
1.2 KiB
Python
Raw Normal View History

2024-03-06 09:49:46 +08:00
#!/usr/bin/env python3
2024-03-06 09:49:46 +08:00
import boto3
import pandas as pd
import csv
2024-03-06 09:49:46 +08:00
client = boto3.client('config')
resp = client.get_discovered_resource_counts()
# print('totalDiscoveredResources', resp['totalDiscoveredResources'], sep=": ")
results = []
2024-03-06 09:49:46 +08:00
for item in resp['resourceCounts']:
paginator = client.get_paginator('list_discovered_resources')
page_iterator = paginator.paginate(resourceType=item['resourceType'])
for page in page_iterator:
for res in page['resourceIdentifiers']:
if item['resourceType'] in [
"AWS::Config::ResourceCompliance",
"AWS::Backup::RecoveryPoint",
"AWS::RDS::DBSnapshot",
"AWS::SSM::ManagedInstanceInventory",
"AWS::SSM::AssociationCompliance",
"AWS::SSM::PatchCompliance",
"AWS::IAM::Policy"
]:
continue
preferResName = res.get('resourceName', res.get('resourceId'))
results += [[item['resourceType'], preferResName]]
df = pd.DataFrame(results, columns=['ResourceType', 'ResourceId'])
# print(df)
print(df.to_csv(index=False, quoting=csv.QUOTE_NONNUMERIC))