import boto3 from datetime import datetime, timedelta import sys import csv def get_clusters() -> list: client = boto3.client('rds') paginator = client.get_paginator('describe_db_clusters') page_iterator = paginator.paginate() clusters = [] for page in page_iterator: for cluster in page['DBClusters']: clusters.append( ( cluster['DBClusterIdentifier'], 'Cluster', cluster['AllocatedStorage'], "".join([tag['Value'] for tag in cluster['TagList'] if tag['Key'] == 'plat_env_project_service']), cluster['BackupRetentionPeriod'] ) ) return clusters def get_instances() -> list: client = boto3.client('rds') paginator = client.get_paginator('describe_db_instances') page_iterator = paginator.paginate() instances = [] for page in page_iterator: for cluster in page['DBInstances']: instance_type = 'Instance' if 'DBClusterIdentifier' not in cluster else 'Cluster' storage_size = cluster['AllocatedStorage'] if cluster['StorageType'] != 'aurora' else get_billed_storage(cluster['DBClusterIdentifier'], cluster['Engine']) instances.append( ( cluster['DBInstanceIdentifier'], instance_type, storage_size, "".join([tag['Value'] for tag in cluster['TagList'] if tag['Key'] == 'plat_env_project_service']), cluster['BackupRetentionPeriod'] ) ) return instances def get_billed_storage(db_cluster_identifier: str, engine: str) -> float: client = boto3.client('cloudwatch') end_time = datetime.now() start_time = end_time - timedelta(days=30) namespace = 'AWS/RDS' if engine != 'docdb' else 'AWS/DocDB' response = client.get_metric_statistics( Namespace=namespace, MetricName='VolumeBytesUsed', Dimensions=[ { 'Name': 'DBClusterIdentifier', 'Value': db_cluster_identifier }, ], StartTime=start_time, EndTime=end_time, Period=86400 * 30, Statistics=[ 'Average', ], Unit='Bytes' ) stat = 0 for datapoint in response['Datapoints']: stat = datapoint['Average'] return bytes_to_gib(stat) def bytes_to_gib(number: int) -> float: return number / 1024 / 1024 / 1024 def get_docdb_snapshots() -> list: client = boto3.client('docdb') db_cluster_paginator = client.get_paginator('describe_db_cluster_snapshots') db_cluster_page_iterator = db_cluster_paginator.paginate() snapshots = [] for page in db_cluster_page_iterator: for db_cluster_snapshot in page['DBClusterSnapshots']: result = client.describe_db_cluster_snapshot_attributes( DBClusterSnapshotIdentifier=db_cluster_snapshot['DBClusterSnapshotIdentifier'] ) snapshots.append( ( db_cluster_snapshot['DBClusterSnapshotIdentifier'], db_cluster_snapshot['DBClusterIdentifier'], 0, "".join([tag['Value'] for tag in db_cluster_snapshot['TagList'] if tag['Key'] == 'plat_env_project_service']) ) ) return snapshots def get_rds_snapshots(): client = boto3.client('rds') db_cluster_paginator = client.get_paginator('describe_db_cluster_snapshots') db_cluster_page_iterator = db_cluster_paginator.paginate() db_paginator = client.get_paginator('describe_db_snapshots') db_page_iterator = db_paginator.paginate() snapshots = [] for page in db_cluster_page_iterator: for db_cluster_snapshot in page['DBClusterSnapshots']: snapshots.append( ( db_cluster_snapshot['DBClusterSnapshotIdentifier'], db_cluster_snapshot['DBClusterIdentifier'], db_cluster_snapshot['AllocatedStorage'], "".join([tag['Value'] for tag in db_cluster_snapshot['TagList'] if tag['Key'] == 'plat_env_project_service']) ) ) for page in db_page_iterator: for db_snapshot in page['DBSnapshots']: snapshots.append( ( db_snapshot['DBSnapshotIdentifier'], db_snapshot['DBInstanceIdentifier'], db_snapshot['AllocatedStorage'], "".join([tag['Value'] for tag in db_snapshot['TagList'] if tag['Key'] == 'plat_env_project_service']) ) ) return snapshots def main(argv): with open('rds-instances.csv', mode='w') as rds_instances_file: rds_instances_writer = csv.writer(rds_instances_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) rds_instances_writer.writerow( ( 'DB_INSTANCE', 'TYPE', 'CLUSTER_STORAGE_SIZE(AVG/MONTH)', 'TAG_VAL', 'BCKP_RETENTION_PERIOD' ) ) rds_instances_writer.writerows(get_instances()) with open('rds-snapshots.csv', mode='w') as rds_snapshots_file: rds_snapshot_writer = csv.writer(rds_snapshots_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) rds_snapshot_writer.writerow( ( 'DB_SNAPSHOT', 'DB_NAME', 'BCKP_STORAGE_SIZE(AVG/MONTH)', 'TAG_VAL' ) ) rds_snapshot_writer.writerows(get_rds_snapshots()) if __name__ == '__main__': sys.exit(main(sys.argv[1:]))