为什么当我有 AWS 积分时 Cost Explorer 客户端显示错误的结果?

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

使用 AWS 控制台 --> AWS 成本管理 --> Cost Explorer 时 - 我得到以下值:

当我使用 @aws-sdk/client-cost-explorer 时,'EC2 - Other' 和 'Amazon Load Balancer' 得到不同的结果。

配置:

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);

结果是:

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"
            }
          }

如您所见,'Amazon ElastiCache' 的数量对于所有指标都是正确的,但是 EC2-Other 的数量对于所有指标都是错误的。

我们的账户目前正在使用 AWS 积分。

我正在寻找用于此 SDK 的正确参数,以便接收每个服务的 daily/monthly 使用率。

默认情况下,控制台中的 Cost Explorer UI 会应用一个过滤器,其中 排除 refundcredit 费用类型(您可以在过滤器列表的中间看到)。

当您使用 AWS 积分时,您当前的 GetCostAndUsageCommand 将包括减少 Amount 值的积分。

您将需要复制已在 UI 中应用的相同排除过滤器。 这将增加您的 Amount 值控制台中显示的内容。

您可以使用 NOT expression. This has been linked to from the GetCostAndUsageCommandInput 文档排除退款 and/or 积分(如适用)。

尝试:

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',
    ]
};