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 billing reports. Is there any java api for getting the same? I couldn't find the same in aws java sdk api documentation.
The AWS SDK for Java simplifies use of AWS Services by providing a set of libraries that are consistent and familiar for Java developers. It provides support for API lifecycle consideration such as credential management, retries, data marshaling, and serialization.
The AWS SDK for Java - Core module holds the classes that are used by the individual service clients to interact with Amazon Web Services. Users need to depend on aws-java-sdk artifact for accessing individual client classes.
The AWS SDK for Java provides a Java API for AWS services. Using the SDK, you can easily build Java applications that work with Amazon S3, Amazon EC2, DynamoDB, and more.
AWS SDK (software development kit) helps simplify your coding by providing JavaScript objects for AWS services. It allows developers to access AWS from JavaScript code that runs directly in the browser, and it includes access to AWS components like Amazon S3, Amazon SNS, Amazon SQS, DynamoDB, and more.
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();
}
}
In Addition to @helloV answer, if you want to view your AWS Billings across days/hours or even minutes. You can use aws-elk-billing tool. Currently the pull request is awaiting to be merged with the main repository. It uses ELK Stack to visualise the AWS Cost and Usage Report
(Although it might still work with AWS Detailed billing report which contains some extra columns along with all the columns from AWS Cost and Usage Report).
Here's a full screenshot of the Kibana Dashboard.
There are no APIs to get AWS billing information. Instead what you can do is:
For more information: See here
Updating the answer as it is no longer the correct one. AWS has released the CostExplorer API for the Java SDK. You can find the documentation here: https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html
public GetCostAndUsageResult getCostAndUsage(GetCostAndUsageRequest request)
Retrieves cost and usage metrics for your account. You can specify which cost and usage-related metric, such as BlendedCosts or UsageQuantity, that you want the request to return. You can also filter and group your data by various dimensions, such as SERVICE or AZ, in a specific time range. For a complete list of valid dimensions, see the GetDimensionValues operation. Master accounts in an organization in AWS Organizations have access to all member accounts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With