import boto3 class AWS: def __init__(self): self.client = boto3.client('ce') def get_cost_and_usage(self, start_str, end_str, region): response = self.client.get_cost_and_usage( TimePeriod={ 'Start': start_str, 'End': end_str }, Metrics=['UnblendedCost'], Granularity='MONTHLY', GroupBy= [ { "Type":"DIMENSION", "Key":"SERVICE" }, { "Type":"DIMENSION", "Key":"USAGE_TYPE" }, ], Filter={ "And":[ {"Dimensions":{ "Key":"REGION", "Values":[region] } }, { "Not":{ "Dimensions":{ "Key":"RECORD_TYPE", "Values":[ "SavingsPlanNegation", "Tax" ] }, } }, { "Not":{ "Or":[ { "Tags":{ "Key":"tenant", "Values":["autonomous-service-clinic"] } }, { "Tags":{ "Key":"tenant", "Values":["autonomous-service-diagnostics"] } }, { "Tags":{ "Key":"tenant", "Values":["tcds"] } } ] } } ] }, ) if "ResultsByTime" not in response or len(response.get('ResultsByTime')) == 0: print("No data") return [] # we get the first of month data return response.get('ResultsByTime')[0]