我想使用Cost Explorer API来获取EC2实例的使用情况和成本。
我能够成功获得使用情况和成本,但是该成本和用途包括EC2实例和EBS卷完成的使用量,因此我想使用来区分成本usage_type_group
,EC2 :Running Hours
但是每当通过时,我都没有任何价值。如果删除,我将获得总使用量和成本的价值。
DimensionValues dimensions = new DimensionValues();
dimensions.setKey("USAGE_TYPE_GROUP");
Collection<String> values = new ArrayList<>();
values.add("EC2 : Running Hours");
dimensions.setValues(values);
filter.setDimensions(dimensions);
GetCostAndUsageRequest getCostAndUsageRequest = new GetCostAndUsageRequest().withGroupBy(groupBy)
.withTimePeriod(dateInterval).withGranularity(Granularity.MONTHLY)
.withFilter(filter)
.withMetrics("UsageQuantity")
.withMetrics("BlendedCost");
try {
costInformation = costExplorer.getCostAndUsage(getCostAndUsageRequest);
} catch (AWSCostExplorerException ex) {
logger.error("cost fetch details failed from cost explorer " + ex.getErrorMessage());
}
试试看 我这样做了,并获得了一些EC2计费信息。
void test_costDetails() {
Expression expression = new Expression();
DimensionValues dimensions = new DimensionValues();
dimensions.withKey(Dimension.SERVICE);
dimensions.withValues("EC2 - Other");
expression.withDimensions(dimensions);
GetCostAndUsageResult result = costExplorer.getCostAndUsage(new GetCostAndUsageRequest().withTimePeriod(
new DateInterval().withStart("2019-10-01").withEnd("2019-10-29")).withGranularity("DAILY").withMetrics(
"BlendedCost").withFilter(expression));
result.getResultsByTime().forEach(r -> {
System.out.println(r);
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句