Search code examples
amazon-web-servicesaws-java-sdkaws-billing

Get AWS resource cost based on particular tag


I've created different AWS resources across EC2, Storage, Load Balancer, Networks etc. I've tagged each resource with GroupName as a key and value which is different for each group e.g. group1, group2, etc.

Now, I want to get cost of each group. I've written following code

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

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

    GetCostAndUsageResult costAndUsage = 
         awsCostExplorer.getCostAndUsage(costAndUsageRequest);

Now, I expect the costAndUsage has groups based on each tag. But it's always giving me the total bill. I can give any random value to withKey but the result is always the same.

Maven dependency:

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

This is the response I'm getting (first resultByTime for brevity)

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

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

What I would like to have is

Cluster1 -> 1500 USD Cluster2 -> 1000 USD

Assuming I have a bunch of resources tagged with key Cluster and values Cluster1 and Cluster2.

There's no grouping by each actual tag value in the response. What am I missing here?


Solution

  • The code was working properly. I was not receiving properly because there are additional steps that needs to be carried out on parent payee account.

    You need to

    1. Go to the AWS billing portal
    2. Select the tag for which you want to enable costing, in my case Cluster
    3. Enable the cost allocation
    4. It will take around 24 hours for this to affect.

    Note: if you have AWS subaccount, your parent payee account will have permission to carry out the above steps.

    See the documentation for more details.


    After 24 hours, if you run the code will see the response as shown below

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