Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the Cost Explorer Client show wrong results when I have AWS credits? [closed]

When using AWS Console --> AWS Cost Management --> Cost Explorer - I get the following values:

enter image description here

When I use @aws-sdk/client-cost-explorer I get different results for 'EC2 - Other' and 'Amazon Load Balancer'.

Configuration:

import { CostExplorerClient, GetCostAndUsageCommand } from '@aws-sdk/client-cost-explorer';

const client = new CostExplorerClient({
        region,
        credentials: {
          accessKeyId,
          secretAccessKey
        }
      });
    
      const params = {
        TimePeriod: {
          Start: startDate,
          End: endDate
        },
        Filter: {
         Dimensions: {
           Key: 'SERVICE',
           Values: [
             'EC2 - Other', 'Amazon ElastiCache'
           ]
         }
        },
        GroupBy: [
          {
            Type: 'DIMENSION',
            Key: 'SERVICE',
          },
        ],
        Granularity: 'DAILY',
        Metrics: [
          'BLENDED_COST',
          'UNBLENDED_COST',
          'AMORTIZED_COST', 
          'NET_AMORTIZED_COST',             
     ] 
      };
      const command = new GetCostAndUsageCommand(params);
      try {
        const data = await client.send(command);
        log.info(data);

And the results are:

GroupDefinitions: [
    {
      "Key": "AZ",
      "Type": "DIMENSION"
    },
    {
      "Key": "SERVICE",
      "Type": "DIMENSION"
    }
  ]
  --
  ResultsByTime: [
    {
      "Estimated": false,
      "Groups": [
        {
          "Keys": [
            "NoAZ",
            "Amazon ElastiCache"
          ],
          "Metrics": {
            "AmortizedCost": {
              "Amount": "-122.4",
              "Unit": "USD"
            },
            "BlendedCost": {
              "Amount": "-122.4",
              "Unit": "USD"
            },
            "NetAmortizedCost": {
              "Amount": "-122.4",
              "Unit": "USD"
            },
            "UnblendedCost": {
              "Amount": "-122.4",
              "Unit": "USD"
            }
          }
        },
        {
          "Keys": [
            "NoAZ",
            "EC2 - Other"
          ],
          "Metrics": {
            "AmortizedCost": {
              "Amount": "0.2467152681",
              "Unit": "USD"
            },
            "BlendedCost": {
              "Amount": "0.2467152681",
              "Unit": "USD"
            },
            "NetAmortizedCost": {
              "Amount": "0.2467152681",
              "Unit": "USD"
            },
            "UnblendedCost": {
              "Amount": "0.2467152681",
              "Unit": "USD"
            }
          }
        },
        {
          "Keys": [
            "us-east-1",
            "Amazon ElastiCache"
          ],
          "Metrics": {
            "AmortizedCost": {
              "Amount": "122.4",
              "Unit": "USD"
            },
            "BlendedCost": {
              "Amount": "122.4",
              "Unit": "USD"
            },
            "NetAmortizedCost": {
              "Amount": "122.4",
              "Unit": "USD"
            },
            "UnblendedCost": {
              "Amount": "122.4",
              "Unit": "USD"
            }
          }
        }
      ],
      "TimePeriod": {
        "End": "2022-05-01",
        "Start": "2022-04-01"
      },
      "Total": {}
    },
    {
      "Estimated": true,
      "Groups": [
        {
          "Keys": [
            "NoAZ",
            "Amazon ElastiCache"
          ],
          "Metrics": {
            "AmortizedCost": {
              "Amount": "-89.59",
              "Unit": "USD"
            },
            "BlendedCost": {
              "Amount": "-89.59",
              "Unit": "USD"
            },
            "NetAmortizedCost": {
              "Amount": "-89.59",
              "Unit": "USD"
            },
            "UnblendedCost": {
              "Amount": "-89.59",
              "Unit": "USD"
            }
          }
        },
        {
          "Keys": [
            "NoAZ",
            "EC2 - Other"
          ],
          "Metrics": {
            "AmortizedCost": {
              "Amount": "0.1766760069",
              "Unit": "USD"
            },
            "BlendedCost": {
              "Amount": "0.1766760069",
              "Unit": "USD"
            },
            "NetAmortizedCost": {
              "Amount": "0.1766760069",
              "Unit": "USD"
            },
            "UnblendedCost": {
              "Amount": "0.1766760069",
              "Unit": "USD"
            }
          }

As you can see the amounts of 'Amazon ElastiCache' are correct for all metrics, but the amount for EC2-Other are wrong for all metrics.

Our account is currently using AWS credits.

I'm looking for the correct parameters to be used for this SDK in order to receive the daily/monthly usage per service.

like image 539
Oron Bendavid Avatar asked Oct 22 '25 03:10

Oron Bendavid


1 Answers

The Cost Explorer UI in the console, by default, applies a filter which excludes refund and credit charge types (which you can see towards the middle of your filter list).

As you're using AWS credits, your current GetCostAndUsageCommand will include credits reducing the Amount value.

You will need to replicate the same exclusion filter, which has been applied in the UI. This will increase your Amount values in line with what is being shown in the console.

You can exclude refunds and/or credits (as applicable) using the NOT expression. This has been linked to from the GetCostAndUsageCommandInput docs.

Try:

const params = {
    TimePeriod: {
        Start: startDate,
        End: endDate
    },
    Filter: {
        Not: {
            Dimensions: {
                Key: "RECORD_TYPE",
                Values: ["Refund", "Credit"]
            }
        },
        Dimensions: {
            Key: 'SERVICE',
            Values: [
                'EC2 - Other', 'Amazon ElastiCache'
            ]
        }
    },
    GroupBy: [{
        Type: 'DIMENSION',
        Key: 'SERVICE',
    }, ],
    Granularity: 'DAILY',
    Metrics: [
        'BLENDED_COST',
        'UNBLENDED_COST',
        'AMORTIZED_COST',
        'NET_AMORTIZED_COST',
    ]
};
like image 191
Ermiya Eskandary Avatar answered Oct 24 '25 18:10

Ermiya Eskandary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!