1

I'm calling boto3 get_cost_and_usage function with below request to fetch daily cost grouped by services and "Name" tag:

result = client.get_cost_and_usage(
    TimePeriod = {
        'Start': '2020-10-31',
        'End': '2020-11-02'
    },
    Granularity = 'DAILY',
    Metrics = ["BlendedCost"],
    GroupBy = [
        {
            'Type': 'DIMENSION',
            'Key': 'SERVICE'
        },
        {
            'Type': 'TAG',
            'Key': 'Name'
        }
    ]
)

In the response, I'm getting all the costs for each service and Name tags for each day, however the total cost for each day is empty [ "Total": {} ]:

"ResultsByTime": [
    {
        "Estimated": true,
        "Groups": [
            {
             ...
            }
        "TimePeriod": {
            "End": "2020-11-01",
            "Start": "2020-10-31"
        },
        "Total": {}
    },
    ...
]

Please tell me if anything is wrong here? I also tried with "UnblendedCost", "AmortizedCost" in Metrics and grouped by only services but it is the same issue i.e., total cost is not coming.

Kindly help me out here.

  • 1
    What appears when you run without the `GroupBy` attribute? – vishal Nov 04 '20 at 13:09
  • @vishal if i don't use any `GroupBy` attribute then the `Total` is coming up in the result. But I actually thought to get the total cost along with the grouped by services costs in one single API call, which it seems to be not possible. – the.realist.guy Nov 06 '20 at 05:39
  • So this proves that the `GroupBy` parameter is the problematic one and it returns no total because it is not able to find any resources according to the groupby parameters given. Hence we need find out the right way to use `GroupBy`. Lemme take a shot at this and will post the solution if possible by today. – vishal Nov 06 '20 at 13:19

2 Answers2

0

I encountered the same issue with Groupby not getting the total amount.

I am calculating the total cost by adding every service cost in the loop like below.

I added line below inside the for loop:

total_cost=float(cost_spent_on_each_service)+total_cost

and declared total_cost = 0 outside of the loop.

Ethan
  • 876
  • 8
  • 18
  • 34
0

This is how I calculated the total cost -

total_cost = 0
result = []
CEclient = handle.client('ce', region_name=region)
try:
    response = CEclient.get_cost_and_usage(
    TimePeriod = {
        'Start': start,
        'End': end
    },
    Granularity='DAILY',
    Metrics = [
        'UnblendedCost',
            ],
    GroupBy=[
        {
            'Type': 'DIMENSION',
            'Key': 'SERVICE'
        },
    ],
    )
except Exception as e:
    raise e
for daily_cost in response['ResultsByTime']:
    date = daily_cost['TimePeriod']['Start']
    for group in daily_cost['Groups']:
        cost_est = {}
        cost_est["date"] = date
        service_name = group['Keys'][0]
        service_cost = group['Metrics']['UnblendedCost']['Amount']
        cost_est["service_name"] = service_name
        cost_est["service_cost"] = service_cost
        total_cost += float(service_cost)
        result.append(cost_est)
print(f"Total Cost: {total_cost}")
return result
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343