# Data points with a period of less than 60 seconds are available for 3 hours. These data points are high-resolution metrics and are available only for custom metrics that have been defined with a StorageResolution of 1. # Data points with a period of 60 seconds (1-minute) are available for 15 days. # Data points with a period of 300 seconds (5-minute) are available for 63 days. # Data points with a period of 3600 seconds (1 hour) are available for 455 days (15 months) # Period # If the StartTime parameter specifies a time stamp that is greater than 3 hours ago, you must specify the period as follows or no data points in that time range is returned: # Start time between 3 hours and 15 days ago - Use a multiple of 60 seconds (1 minute). # Start time between 15 and 63 days ago - Use a multiple of 300 seconds (5 minutes). # Start time greater than 63 days ago - Use a multiple of 3600 seconds (1 hour). # Write request units $1.25 per million write request units # Read request units $0.25 per million read request units # Provisioned Throughput Type Price per hour # Write capacity unit (WCU) $0.00065 per WCU # Read capacity unit (RCU) $0.00013 per RCU ONDEMAND_WPM = 1.25 ONDEMAND_RPM = 0.25 PROVISIONED_WCU = 0.00065 PROVISIONED_RCU = 0.00013 import boto3 import datetime savings_total = 0 month_days = 31 endTime = datetime.datetime(2020, 8, 20) startTime = endTime - datetime.timedelta(28) table = 'prod-masters_audit_new' cloudwatch = boto3.resource('cloudwatch', region_name='us-east-1') dynamodb = boto3.resource('dynamodb', region_name='us-east-1') savings = dict() #cloudwatch = boto3.client('cloudwatch', region_name='us-east-1') dynamo_client = boto3.client('dynamodb', region_name='us-east-1') dynamo_table_list = dynamo_client.list_tables() import pprint tables = [] tables.extend(dynamo_table_list['TableNames']) while dynamo_table_list.get('LastEvaluatedTableName') is not None: dynamo_table_list = dynamo_client.list_tables(ExclusiveStartTableName=dynamo_table_list['LastEvaluatedTableName']) tables.extend(dynamo_table_list['TableNames']) for table in tables: dynamo_table = dynamodb.Table(table) if dynamo_table.billing_mode_summary is not None: continue dimensions = [{'Name': 'TableName', 'Value': table} ] metric = cloudwatch.Metric("AWS/DynamoDB", "ConsumedReadCapacityUnits") all_the_metrics = metric.get_statistics( Dimensions=dimensions, StartTime=startTime, EndTime=endTime, Period=7200, Statistics=[ 'Sum', ] ) consumed_read = 0 for metrics in all_the_metrics['Datapoints']: consumed_read+=metrics['Sum'] metric = cloudwatch.Metric("AWS/DynamoDB", "ConsumedWriteCapacityUnits") all_the_metrics = metric.get_statistics( Dimensions=dimensions, StartTime=startTime, EndTime=endTime, Period=7200, Statistics=[ 'Sum', ] ) consumed_write = 0 for metrics in all_the_metrics['Datapoints']: consumed_write+=metrics['Sum'] # print(consumed_write) # print('consumed {} read and {} write'.format(consumed_read, consumed_write)) ondemand_pricing = (consumed_read/1000000 * ONDEMAND_RPM + consumed_write/1000000 * ONDEMAND_WPM) # print('ondemand pricing {}'.format(ondemand_pricing)) provisioned_read_capacity = dynamo_table.provisioned_throughput['ReadCapacityUnits'] provisioned_write_capacity = dynamo_table.provisioned_throughput['WriteCapacityUnits'] provisioned_pricing = (provisioned_read_capacity * PROVISIONED_RCU + provisioned_write_capacity * PROVISIONED_WCU) * 24 * month_days # print('provisioned pricing {}'.format(provisioned_pricing)) price_diff = provisioned_pricing - ondemand_pricing # print('Savings for table {}: {}'.format(table, price_diff)) print('{} {}'.format(price_diff, table)) savings[table] = price_diff savings_total+=price_diff pprint.pprint(savings) print(savings_total)