Different Total Cost in Cost Explorer vs API

4

Hi,

I'm having an issue related to a discrepancy between what is shown in the Cost Explorer and Billing pages in the AWS console vs what is returned over the API.

Previous to March 2023, both views (console and API) would show the same total cost. March 2023 was the first month we implemented a Savings Plan so we changed from using the Unblended data set to using Amortized (this change was applied to both our console settings and API usage) - I believe the Savings Plan is the source of this issue somehow, since it just showed up this month.

Viewing our cost for March 2023 in Cost Explorer shows the total cost as $17,409 as seen here:

Cost in console

However, when attempting to pull the same data over the API, it shows $16,843 as the total cost for the month. The code used to query the API is the following in Python:

    client = boto3.client("ce")
    total_cost = client.get_cost_and_usage(
        TimePeriod={"Start": '2023-03-01', "End": '2023-03-31'},
        Granularity="MONTHLY",
        Metrics=["AmortizedCost"],
    )["ResultsByTime"]
    print(total_cost)
# -> [{'TimePeriod': {'Start': '2023-03-01', 'End': '2023-03-31'}, 'Total': {'AmortizedCost': {'Amount': '16843.7283177265', 'Unit': 'USD'}}, 'Groups': [], 'Estimated': False}]

This discrepancy exists no matter which data set I use (unblended or amortized) and also only exists for March 2023.

Does anyone have insight on how I might rectify? We rely on the API for running some cost analysis that can't be done through the console but if it's giving inaccurate costs we can't use it.

Thanks!

mburger
已提问 1 年前1168 查看次数
2 回答
2
已接受的回答

Hello mburger,

You're finding different values because CE API deals with End (in TimePeriod) as exclusive. The boto3 documentation says:

The end of the time period that you want the usage and costs for. The end date is exclusive. For example, if end is 2017-05-01 , AWS retrieves cost and usage data from the start date up to, but not including, 2017-05-01.

With that in place, promote the following changes in your code to find the same you saw at the screenshot:

    client = boto3.client("ce")
    total_cost = client.get_cost_and_usage(
        TimePeriod={"Start": '2023-03-01', "End": '2023-04-01'},
        Granularity="MONTHLY",
        Metrics=["AmortizedCost"],
    )["ResultsByTime"]
    print(total_cost)

After test it, share your findings!

AWS
已回答 1 年前
profile picture
专家
已审核 9 个月前
profile pictureAWS
专家
已审核 10 个月前
  • Yes, that brings the numbers into alignment, thank you for your help!

-1

Hello, check if Cost Explorer you're seeing it Aggregating costs by 'Amortized' (in Advanced options section). It seems that the screenshot was get with Unblended (the default).

AWS
已回答 1 年前
  • Yes - Cost Explorer shows the same number regardless of whether using "Amortized" or "Unblended" data sets. The screenshot was taken using "Amortized".

    Worth noting that the API also reports the same cost regardless of which basis is used (Amortized or Unblended)

    I've updated the screenshot to show the advanced settings as well.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则