I\'m trying to get the billing information from aws for ec2 instances, s3 buckets and ebs volumes using java api. I want to create api that gives specific entity wise hourly
You can get Cost and Usage Data using AWS Java SDK. Here is a functional sample.
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.costexplorer.AWSCostExplorer;
import com.amazonaws.services.costexplorer.AWSCostExplorerClientBuilder;
import com.amazonaws.services.costexplorer.model.DateInterval;
import com.amazonaws.services.costexplorer.model.GetCostAndUsageRequest;
import com.amazonaws.services.costexplorer.model.GetCostAndUsageResult;
public class AwsCostExplorer {
private static AWSCostExplorer awsCostExplorerClient;
public static void main(String arg[]){
AWSCostExplorerClientBuilder builder =AWSCostExplorerClientBuilder.standard();
awsCostExplorerClient = builder.withCredentials(new AWSStaticCredentialsProvider(new ProfileCredentialsProvider("profile-name").getCredentials()))
.withRegion(Regions.US_EAST_1).build();
GetCostAndUsageRequest request = new GetCostAndUsageRequest()
.withTimePeriod(new DateInterval().withStart("2018-07-01").withEnd("2018-07-25"))
.withGranularity("DAILY")
.withMetrics("BlendedCost");
GetCostAndUsageResult result = awsCostExplorerClient.getCostAndUsage(request);
result.getResultsByTime().forEach(resultByTime -> {
System.out.println(resultByTime.toString());
});
awsCostExplorerClient.shutdown();
}
}