aws billing information using aws java sdk

后端 未结 5 1052
不知归路
不知归路 2020-11-29 10:32

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

5条回答
  •  执笔经年
    2020-11-29 11:14

    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();
        }
    }
    

提交回复
热议问题