根据特定标签获取AWS资源成本

时间:2018-11-29 10:42:23

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

我已经跨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标记的资源。

响应中没有按每个实际标签值进行分组。 我在这里想念什么?

2 个答案:

答案 0 :(得分:0)

我不确定您使用的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

它可以为您添加\ edit的内容提供更好的指导。 如果您可以共享文档和得到的答复,则可以编辑和添加更多相关信息。

答案 1 :(得分:0)

代码正常运行。我收货不正确,因为需要对父收款人帐户执行其他步骤。

您需要

  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
}
相关问题