1

我想使用 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());
        }
4

1 回答 1

3

试试这个。我这样做并获得了一些 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);
        });
    }
于 2019-10-29T08:54:55.000 回答