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
asked a year ago1138 views
2 Answers
2
Accepted Answer

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
answered a year ago
profile picture
EXPERT
reviewed 8 months ago
profile pictureAWS
EXPERT
reviewed 9 months ago
  • 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
answered a year ago
  • 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.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions