Manohar
Manohar

Reputation: 127

How to get AWS Billing metrics through JAVA API

I had found the AWS cloud watch collects Billing metrics, but couldn't find any api references to use them programmatically. I want them as metrics just like Volume metrics, Instance metrics but not as CSV format in S3 bucket. Is there any way to achieve this?

Upvotes: 1

Views: 1410

Answers (1)

imTachu
imTachu

Reputation: 3809

First, two things to keep in mind:

  1. According to this documentation, the maximum number of data points returned from a single GetMetricStatistics request is 1,440. So you can't for example, query data from a week in periods of 5 minutes (cause that will be 2,016 datapoints).
  2. To get the billing metrics you can request the total of estimated charges across all services or the estimated charges per service. As stated here.

This Java 8 sample, retrieves the total of estimated charges across all services for the last two weeks in periods of twelve hours.

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;
import com.amazonaws.services.cloudwatch.model.Datapoint;
import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult;
import java.util.Collections;
import java.util.Date;

public class AWSCloudWatchBillingService { 

    public static void main(String[] args) {
        final String awsAccessKey = "<YOUR_AWS_ACCESS_KEY>";
        final String awsSecretKey = "<YOUR_AWS_SECRET_ACCESS_KEY>";

        final AmazonCloudWatchClient client = client(awsAccessKey, awsSecretKey);
        final GetMetricStatisticsRequest request = request(); 
        final GetMetricStatisticsResult result = result(client, request);
        printIt(result);   
    }

    private static AmazonCloudWatchClient client(final String awsAccessKey, final String awsSecretKey) {
        final AmazonCloudWatchClient client = new AmazonCloudWatchClient(new BasicAWSCredentials(awsAccessKey, awsSecretKey));
        client.setEndpoint("http://monitoring.us-east-1.amazonaws.com/");
        return client;
    }

    private static GetMetricStatisticsRequest request() {
        final long twoWeeks = 1000 * 60 * 60 * 24 * 15;
        final int twelveHours = 60 * 60 * 12;
        return new GetMetricStatisticsRequest()
            .withStartTime(new Date(new Date().getTime() - twoWeeks))
            .withNamespace("AWS/Billing")
            .withPeriod(twelveHours)
            .withDimensions(new Dimension().withName("Currency").withValue("USD"))
            .withMetricName("EstimatedCharges")
            .withStatistics("Average", "Maximum")
            .withEndTime(new Date());
    }

    private static GetMetricStatisticsResult result(
            final AmazonCloudWatchClient client, final GetMetricStatisticsRequest request) {
         return client.getMetricStatistics(request);
    }

    private static void printIt(final GetMetricStatisticsResult result) {
        Collections.sort(result.getDatapoints(), (Datapoint dp1, Datapoint dp2) -> dp1.getTimestamp().compareTo(dp2.getTimestamp()));
        System.out.println("**************************************"); 
        System.out.println(result);
    }
}

Upvotes: 2

Related Questions