代码之家  ›  专栏  ›  技术社区  ›  Ganesh Satpute

基于特定标签获取AWS资源成本

  •  0
  • Ganesh Satpute  · 技术社区  · 6 年前

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

    现在,我想知道每组的成本。我写了以下代码

        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 价值观 Cluster1 Cluster2 .

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

    2 回复  |  直到 6 年前
        1
  •  0
  •   Dvir669    6 年前

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

    它可以让您更好地了解需要添加/编辑的内容。 如果您可以共享您的文档和得到的响应,那么编辑和添加更多相关信息将很有帮助。

        2
  •  0
  •   Ganesh Satpute    6 年前

    代码工作正常。我没有收到正确的,因为有额外的步骤需要在父收款人帐户上执行。

    你需要

    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
    }
    
    推荐文章