./aws-cost-and-usage-report.py
TimePeriod LinkedAccount Service Amount Unit Estimated
2019-11-08 21212121212121 AWS CloudTrail 0.153943 USD False
2019-11-08 21212121212121 AWS Config 9.213 USD False
2019-11-08 21212121212121 AWS Direct Connect 0.2797877163 USD False
2019-11-08 21212121212121 AWS Key Management Service 1.4141780112 USD False
2019-11-08 21212121212121 AWS Lambda 0.0804225759 USD False
2019-11-08 21212121212121 Amazon DynamoDB 0.3836161225 USD False
2019-11-08 21212121212121 Amazon EC2 Container Registry (ECR) 0.0783308328 USD False
2019-11-08 21212121212121 Amazon EC2 Container Service 0 USD False
2019-11-08 21212121212121 EC2 - Other 6.8639388761 USD False
2019-11-08 21212121212121 Amazon Elastic Compute Cloud - Compute 73.1890902202 USD False
2019-11-08 21212121212121 Amazon Elastic File System 2.2110942898 USD False
2019-11-08 21212121212121 Amazon Elastic Load Balancing 4.8388505022 USD False
2019-11-08 21212121212121 Amazon GuardDuty 0.761623977 USD False
2019-11-08 21212121212121 Amazon Relational Database Service 21.2797291955 USD False
#!/usr/bin/env python3
import argparse
import boto3
import datetime
parser = argparse.ArgumentParser()
parser.add_argument('--days', type=int, default=30)
args = parser.parse_args()
now = datetime.datetime.utcnow()
start = (now - datetime.timedelta(days=args.days)).strftime('%Y-%m-%d')
end = now.strftime('%Y-%m-%d')
template="{10} {14} {40} {16.10} {6} {10}"
cd = boto3.client('ce', 'ap-southeast-2')
results = []
token = None
while True:
if token:
kwargs = {'NextPageToken': token}
else:
kwargs = {}
data = cd.get_cost_and_usage(TimePeriod={'Start': start, 'End': end}, Granularity='DAILY', Metrics=['UnblendedCost'], GroupBy=[{'Type': 'DIMENSION', 'Key': 'LINKED_ACCOUNT'}, {'Type': 'DIMENSION', 'Key': 'SERVICE'}], **kwargs)
results += data['ResultsByTime']
token = data.get('NextPageToken')
if not token:
break
# print('\t'.join(['TimePeriod', 'LinkedAccount', 'Service'.ljust(40, ' '), 'Amount', 'Unit', 'Estimated']))
print(template.format("TimePeriod", "LinkedAccount", "Service", "Amount", "Unit", "Estimated")
for result_by_time in results:
for group in result_by_time['Groups']:
amount = group['Metrics']['UnblendedCost']['Amount']
unit = group['Metrics']['UnblendedCost']['Unit']
# comp = join(group['Keys'])
# rep_start_time = result_by_time['TimePeriod']['Start']
# print(rep_start_time.ljust(10, ' '), '\t', '\t' .join(group['Keys']), '\t', '\t', '\t', amount.rjust(30, ' '), '\t', unit, '\t', result_by_time['Estimated'])
print(template.format(result_by_time['TimePeriod']['Start'], group['Keys'], amount, '\t', unit, '\t', result_by_time['Estimated'])
./aws-cost-and-usage-report.py --days=7
Traceback (most recent call last):
File "./aws-cost-and-usage-report.py", line 34, in <module>
print(template.format("TimePeriod", "LinkedAccount", "Service", "Amount", "Unit", "Estimated"))
IndexError: tuple index out of range
#!/usr/bin/env python3
import argparse
import boto3
import datetime
parser = argparse.ArgumentParser()
parser.add_argument('--days', type=int, default=30)
args = parser.parse_args()
now = datetime.datetime.utcnow()
start = (now - datetime.timedelta(days=args.days)).strftime('%Y-%m-%d')
end = now.strftime('%Y-%m-%d')
dtemplate="{:10} {:14} {:40} {:16.10f} {:6} {:10}"
htemplate="{:10} {:14} {:40} {:16} {:6} {:10}"
cd = boto3.client('ce', 'ap-southeast-2')
results = []
token = None
while True:
if token:
kwargs = {'NextPageToken': token}
else:
kwargs = {}
data = cd.get_cost_and_usage(TimePeriod={'Start': start, 'End': end}, Granularity='DAILY', Metrics=['UnblendedCost'], GroupBy=[{'Type': 'DIMENSION', 'Key': 'LINKED_ACCOUNT'}, {'Type': 'DIMENSION', 'Key': 'SERVICE'}], **kwargs)
results += data['ResultsByTime']
token = data.get('NextPageToken')
if not token:
break
# print('\t'.join(['TimePeriod', 'LinkedAccount', 'Service'.ljust(40, ' '), 'Amount', 'Unit', 'Estimated']))
print(htemplate.format("TimePeriod", "LinkedAccount", "Service", "Amount", "Unit", "Estimated"))
for result_by_time in results:
for group in result_by_time['Groups']:
amount = group['Metrics']['UnblendedCost']['Amount']
unit = group['Metrics']['UnblendedCost']['Unit']
# comp = join(group['Keys'])
# rep_start_time = result_by_time['TimePeriod']['Start']
# print(rep_start_time.ljust(10, ' '), '\t', '\t' .join(group['Keys']), '\t', '\t', '\t', amount.rjust(30, ' '), '\t', unit, '\t', result_by_time['Estimated'])
print(dtemplate.format(result_by_time['TimePeriod']['Start'], group['Keys']['LINKED_ACCOUNT'], group['Keys']['SERVICE'], amount, unit, result_by_time['Estimated']))
print(dtemplate.format(result_by_time['TimePeriod']['Start'], group['Keys'][0], group['Keys'][1], amount, unit, result_by_time['Estimated']))
TimePeriod LinkedAccount Service Amount Unit Estimated
Traceback (most recent call last):
File "./aws-cost-and-usage-report.py", line 45, in <module>
print(dtemplate.format(result_by_time['TimePeriod']['Start'], group['Keys'][0], group['Keys'][1], amount, unit, result_by_time['Estimated']))
ValueError: Unknown format code 'f' for object of type 'str'
Then use for heading & data:
Open in new window
You still need the subfields for the keys to be corrected i guessed them from the source.The format might need a bit of adjustment. I guessed the widths from the above example.