4

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.

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
Oron Bendavid
  • 1,485
  • 3
  • 18
  • 34
  • Since you have AWS credits, this may be skewing the unblended costs so try the above and let me know if that works. AWS credits are also applied at the beginning of the month so net amortised is your best bet. – Ermiya Eskandary May 23 '22 at 07:07
  • Hey @ErmiyaEskandary thanks for your answer, I updated the question with some more details regarding the wrong amounts. – Oron Bendavid May 23 '22 at 07:51
  • I'm using credits as part of the AWS startup loft accelerator (only in one place). I am pretty sure that I have configured to use spots when possible. Another thing to think about, is that I was able to receive the "UsageQuantity" per month (1499.2887292141) and tried to multiple it with the AmortizedCost (0.2467152681) the result is wrong, but maybe it needs to be sort of calculation – Oron Bendavid May 23 '22 at 08:00
  • 1
    I understand - can you see in the right hand side of your screen the ‘Credits’ are being excluded showing the real cost? You’re missing this filter from your SDK calls. Add this to your filter block & try again: `"Not":{"Dimensions":{"Key":"RECORD_TYPE","Values":["Refund","Credit"]}}` to replicate your UI :) let me know how it goes. – Ermiya Eskandary May 23 '22 at 08:09
  • BOOM ! Yeah ! Thank you :) Can you please share the reference to the docs of the 'Not' option ? – Oron Bendavid May 23 '22 at 08:24
  • 1
    You're welcome - I've added an answer with documentation references as you've requested :) glad you got it sorted. – Ermiya Eskandary May 23 '22 at 09:51

1 Answers1

4

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',
    ]
};
Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44