【问题标题】:Get AWS resource cost based on particular tag根据特定标签获取 AWS 资源成本
【发布时间】:2018-11-29 10:42:22
【问题描述】:

我在 EC2、存储、负载均衡器、网络等方面创建了不同的 AWS 资源。我将每个资源标记为 GroupName 作为键和值,每个组都不同,例如group1group2

现在,我想获取每个组的成本。我写了以下代码

    GroupDefinition groupDefinition =
        new GroupDefinition().withType(GroupDefinitionType.TAG).withKey("Cluster");

    GetCostAndUsageRequest costAndUsageRequest
        = new GetCostAndUsageRequest()
          .withGroupBy(groupDefinition)
          .withGranularity("MONTHLY")
          .withMetrics("UnblendedCost");

    GetCostAndUsageResult costAndUsage = 
         awsCostExplorer.getCostAndUsage(costAndUsageRequest);

现在,我希望 costAndUsage 具有基于每个标签的组。但它总是给我总账单。我可以给withKey 任何随机值,但结果总是一样的。

Maven 依赖:

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.11.453</version>
    </dependency>

这是我收到的回复(为简洁起见,首先是 resultByTime

{
   "timePeriod":{
      "start":"2018-11-01",
      "end":"2018-12-01"
   },
   "total":{

   },
   "groups":[
      {
         "keys":[
            "Cluster$"
         ],
         "metrics":{
            "UnblendedCost":{
               "amount":"26712.9751185906",
               "unit":"USD"
            }
         }
      }
   ],
   "estimated":false
}

我想要的是

Cluster1 -> 1500 USD Cluster2 -> 1000 USD

假设我有一堆资源标记有键 Cluster 和值 Cluster1Cluster2

响应中没有按每个实际标记值分组。 我在这里错过了什么?

【问题讨论】:

标签: amazon-web-services aws-java-sdk aws-billing


【解决方案1】:

我不确定您使用的 SDK 的实际版本是什么,但我可以向您展示 Ruby 中的工作代码(我也按 AWS 服务分组):

credentials = Aws::SharedCredentials.new(profile_name: "REPLACE_profile_name_in_credentials_file")
client = Aws::CostExplorer::Client.new(region: "us-east-1", credentials: credentials)
resp = client.get_cost_and_usage({
      time_period: {
        start: Date.today.at_beginning_of_month.strftime("%Y-%m-%d"), 
        end: Date.tomorrow.strftime("%Y-%m-%d"), 
      },
      granularity: "MONTHLY", 
      metrics: ["UnblendedCost"],
      group_by: [
        {
          type: "DIMENSION", 
          key: "SERVICE",
        },
        {
          type: "TAG",
          key: "Cluster",  
        }
      ]
    })
resp.results_by_time.each do |result|
  #DO SOMETHING
end

它可能会给你一个更好的方向,你需要添加\编辑。 如果您可以分享您的文档和收到的回复,那么编辑和添加更多相关信息将很有帮助。

【讨论】:

  • 感谢@Dvir669 的回答。我不想聚合服务。是否可以检查按字段删除SERVICE 后是否有效?另外,如果将值更改为Cluster1,你会得到相同的输出吗?另外,您是否在 AWS 账单门户上启用了 Cluster 标签?
  • 另外,我尝试了与您使用的 API 类似的方式。我发现,无论我是否发送 TAG 字段,我都会得到相同的输出。
  • 我看不到您对代码中的响应做了什么 - 请注意,来自该 API 的任何响应都有一个“ResultsByTime”->“Groups”->“Keys”需要访问,而不是“总计”字段。
  • 您可以删除“服务”条款,它仍然可以工作。请注意,您需要使用标签的“键”,而不是标签的“值”
  • 是的。这正是我正在做的事情(根据你的最后两个 cmets)。问题不在于响应,问题在于我得到的值。无论我作为标签键传递什么,它们都是相同的。逻辑上这就是我想要的:Cluster1 -&gt; 1500 USDCluster2 -&gt; 1000 USD 当然,这里Cluster1Cluster2 是名称为Cluster 的标签的值。
【解决方案2】:

代码运行正常。我没有正确接收,因为需要在父收款人帐户上执行其他步骤。

你需要

  1. 转到 AWS 账单门户
  2. 选择要启用成本计算的标签,在我的例子中是Cluster
  3. 启用成本分配
  4. 这需要大约 24 小时才能生效。

注意:如果您有 AWS 子账户,您的父收款人账户将有权执行上述步骤。

有关详细信息,请参阅documentation


24小时后,如果你运行代码会看到如下所示的响应

{
   "timePeriod":{
      "start":"2018-12-01",
      "end":"2018-12-31"
   },
   "total":{

   },
   "groups":[
      {
         "keys":[
            "Cluster$"
         ],
         "metrics":{
            "UnblendedCost":{
               "amount":"23434.5469766795",
               "unit":"USD"
            }
         }
      },
      {
         "keys":[
            "Cluster$cluster-1"
         ],
         "metrics":{
            "UnblendedCost":{
               "amount":"2343.3888413893",
               "unit":"USD"
            }
         }
      },
      {
         "keys":[
            "Cluster$cluster-2"
         ],
         "metrics":{
            "UnblendedCost":{
               "amount":"23464.8057597427",
               "unit":"USD"
            }
         }
      }
   ],
   "estimated":true
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多