Skip to content

get_cost_and_usage_with_resources async

get_cost_and_usage_with_resources(
    time_period: DateInterval,
    granularity: Granularity,
    filter: Expression,
    *,
    config_overrides: Optional[AsyncCostExplorerClientConfig] = None,
    metrics: Optional[MetricNames] = None,
    group_by: Optional[GroupDefinitions] = None,
    billing_view_arn: Optional[BillingViewArn] = None,
    next_page_token: Optional[NextPageToken] = None,
) -> GetCostAndUsageWithResourcesResponse

Retrieves cost and usage metrics with resources 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. Management account in an organization in Organizations have access to all member accounts.

Hourly granularity is only available for EC2-Instances (Elastic Compute Cloud) resource-level data. All other resource-level data is available at daily granularity.

This is an opt-in only feature. You can enable this feature from the Cost Explorer Settings page. For information about how to access the Settings page, see Controlling Access for Cost Explorer in the Billing and Cost Management User Guide.

Parameters:

  • time_period (DateInterval) –

    Sets the start and end dates for retrieving Amazon Web Services costs. The range must be within the last 14 days (the start date cannot be earlier than 14 days ago). The start date is inclusive, but the end date is exclusive. For example, if start is 2017-01-01 and end is 2017-05-01, then the cost and usage data is retrieved from 2017-01-01 up to and including 2017-04-30 but not including 2017-05-01.

  • granularity (Granularity) –

    Sets the Amazon Web Services cost granularity to MONTHLY, DAILY, or HOURLY. If Granularity isn't set, the response object doesn't include the Granularity, MONTHLY, DAILY, or HOURLY.

  • filter (Expression) –

    Filters Amazon Web Services costs by different dimensions. For example, you can specify SERVICE and LINKED_ACCOUNT and get the costs that are associated with that account's usage of that service. You can nest Expression objects to define any combination of dimension filters. For more information, see Expression.

    The GetCostAndUsageWithResources operation requires that you either group by or filter by a ResourceId. It requires the Expression "SERVICE = Amazon Elastic Compute Cloud - Compute" in the filter.

    Valid values for MatchOptions for Dimensions are EQUALS and CASE_SENSITIVE.

    Valid values for MatchOptions for CostCategories, Tags, and ProductAttributes are EQUALS, ABSENT, and CASE_SENSITIVE. Default values are EQUALS and CASE_SENSITIVE.

    If you filter or group by product attributes, the SERVICE filter rules are the same as for GetCostAndUsage.

  • metrics (Optional[MetricNames], default: None ) –

    Which metrics are returned in the query. For more information about blended and unblended rates, see Why does the "blended" annotation appear on some line items in my bill?.

    Valid values are AmortizedCost, BlendedCost, NetAmortizedCost, NetUnblendedCost, NormalizedUsageAmount, UnblendedCost, and UsageQuantity.

    If you return the UsageQuantity metric, the service aggregates all usage numbers without taking the units into account. For example, if you aggregate usageQuantity across all of Amazon EC2, the results aren't meaningful because Amazon EC2 compute hours and data transfer are measured in different units (for example, hour or GB). To get more meaningful UsageQuantity metrics, filter by UsageType or UsageTypeGroups.

    Metrics is required for GetCostAndUsageWithResources requests.

  • group_by (Optional[GroupDefinitions], default: None ) –

    You can group Amazon Web Services costs using up to two different groups: DIMENSION, TAG, COST_CATEGORY, and PRODUCT_ATTRIBUTE.

    PRODUCT_ATTRIBUTE groups work the same way as in GetCostAndUsage. A PRODUCT_ATTRIBUTE group or a ProductAttributes filter doesn't meet the requirement to group by or filter by a ResourceId.

  • billing_view_arn (Optional[BillingViewArn], default: None ) –

    The Amazon Resource Name (ARN) that uniquely identifies a specific billing view. The ARN is used to specify which particular billing view you want to interact with or retrieve information from when making API calls related to Amazon Web Services Billing and Cost Management features. The BillingViewArn can be retrieved by calling the ListBillingViews API.

  • next_page_token (Optional[NextPageToken], default: None ) –

    The token to retrieve the next set of results. Amazon Web Services provides the token when the response from a previous call has more results than the maximum page size.

Raises:

Source code in docs_src/capo_cost_explorer/_services/async_cost_explorer.py
async def get_cost_and_usage_with_resources(
    self,
    time_period: "capo_cost_explorer.types.date_interval.DateInterval",
    granularity: "capo_cost_explorer.types.granularity.Granularity",
    filter: "capo_cost_explorer.types.expression.Expression",
    *,
    config_overrides: Optional[AsyncCostExplorerClientConfig] = None,
    metrics: Optional["capo_cost_explorer.types.metric_names.MetricNames"] = None,
    group_by: Optional[
        "capo_cost_explorer.types.group_definitions.GroupDefinitions"
    ] = None,
    billing_view_arn: Optional[
        "capo_cost_explorer.types.billing_view_arn.BillingViewArn"
    ] = None,
    next_page_token: Optional[
        "capo_cost_explorer.types.next_page_token.NextPageToken"
    ] = None,
) -> "capo_cost_explorer.types.get_cost_and_usage_with_resources_response.GetCostAndUsageWithResourcesResponse":
    """<p>Retrieves cost and usage metrics with resources for your account. You can specify which cost and usage-related metric, such as <code>BlendedCosts</code> or <code>UsageQuantity</code>, that you want the request to return. You can also filter and group your data by various dimensions, such as <code>SERVICE</code> or <code>AZ</code>, in a specific time range. For a complete list of valid dimensions, see the <a href="https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetDimensionValues.html">GetDimensionValues</a> operation. Management account in an organization in Organizations have access to all member accounts.</p> <p>Hourly granularity is only available for EC2-Instances (Elastic Compute Cloud) resource-level data. All other resource-level data is available at daily granularity.</p> <note> <p>This is an opt-in only feature. You can enable this feature from the Cost Explorer Settings page. For information about how to access the Settings page, see <a href="https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/ce-access.html">Controlling Access for Cost Explorer</a> in the <i>Billing and Cost Management User Guide</i>.</p> </note>

    Args:
        time_period: <p>Sets the start and end dates for retrieving Amazon Web Services costs. The range must be within the last 14 days (the start date cannot be earlier than 14 days ago). The start date is inclusive, but the end date is exclusive. For example, if <code>start</code> is <code>2017-01-01</code> and <code>end</code> is <code>2017-05-01</code>, then the cost and usage data is retrieved from <code>2017-01-01</code> up to and including <code>2017-04-30</code> but not including <code>2017-05-01</code>.</p>
        granularity: <p>Sets the Amazon Web Services cost granularity to <code>MONTHLY</code>, <code>DAILY</code>, or <code>HOURLY</code>. If <code>Granularity</code> isn't set, the response object doesn't include the <code>Granularity</code>, <code>MONTHLY</code>, <code>DAILY</code>, or <code>HOURLY</code>. </p>
        filter: <p>Filters Amazon Web Services costs by different dimensions. For example, you can specify <code>SERVICE</code> and <code>LINKED_ACCOUNT</code> and get the costs that are associated with that account's usage of that service. You can nest <code>Expression</code> objects to define any combination of dimension filters. For more information, see <a href="https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_Expression.html">Expression</a>. </p> <p>The <code>GetCostAndUsageWithResources</code> operation requires that you either group by or filter by a <code>ResourceId</code>. It requires the <a href="https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_Expression.html">Expression</a> <code>"SERVICE = Amazon Elastic Compute Cloud - Compute"</code> in the filter.</p> <p>Valid values for <code>MatchOptions</code> for <code>Dimensions</code> are <code>EQUALS</code> and <code>CASE_SENSITIVE</code>.</p> <p>Valid values for <code>MatchOptions</code> for <code>CostCategories</code>, <code>Tags</code>, and <code>ProductAttributes</code> are <code>EQUALS</code>, <code>ABSENT</code>, and <code>CASE_SENSITIVE</code>. Default values are <code>EQUALS</code> and <code>CASE_SENSITIVE</code>.</p> <p>If you filter or group by product attributes, the <code>SERVICE</code> filter rules are the same as for <a href="https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetCostAndUsage.html">GetCostAndUsage</a>.</p>
        metrics: <p>Which metrics are returned in the query. For more information about blended and unblended rates, see <a href="http://aws.amazon.com/premiumsupport/knowledge-center/blended-rates-intro/">Why does the "blended" annotation appear on some line items in my bill?</a>. </p> <p>Valid values are <code>AmortizedCost</code>, <code>BlendedCost</code>, <code>NetAmortizedCost</code>, <code>NetUnblendedCost</code>, <code>NormalizedUsageAmount</code>, <code>UnblendedCost</code>, and <code>UsageQuantity</code>. </p> <note> <p>If you return the <code>UsageQuantity</code> metric, the service aggregates all usage numbers without taking the units into account. For example, if you aggregate <code>usageQuantity</code> across all of Amazon EC2, the results aren't meaningful because Amazon EC2 compute hours and data transfer are measured in different units (for example, hour or GB). To get more meaningful <code>UsageQuantity</code> metrics, filter by <code>UsageType</code> or <code>UsageTypeGroups</code>. </p> </note> <p> <code>Metrics</code> is required for <code>GetCostAndUsageWithResources</code> requests.</p>
        group_by: <p>You can group Amazon Web Services costs using up to two different groups: <code>DIMENSION</code>, <code>TAG</code>, <code>COST_CATEGORY</code>, and <code>PRODUCT_ATTRIBUTE</code>.</p> <p> <code>PRODUCT_ATTRIBUTE</code> groups work the same way as in <a href="https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetCostAndUsage.html">GetCostAndUsage</a>. A <code>PRODUCT_ATTRIBUTE</code> group or a <code>ProductAttributes</code> filter doesn't meet the requirement to group by or filter by a <code>ResourceId</code>.</p>
        billing_view_arn: <p>The Amazon Resource Name (ARN) that uniquely identifies a specific billing view. The ARN is used to specify which particular billing view you want to interact with or retrieve information from when making API calls related to Amazon Web Services Billing and Cost Management features. The BillingViewArn can be retrieved by calling the ListBillingViews API.</p>
        next_page_token: <p>The token to retrieve the next set of results. Amazon Web Services provides the token when the response from a previous call has more results than the maximum page size.</p>

    Raises:
        capo_cost_explorer.errors.bill_expiration_exception.BillExpirationException: <p>The requested report expired. Update the date interval and try again.</p>
        capo_cost_explorer.errors.billing_view_health_status_exception.BillingViewHealthStatusException: <p> The billing view status must be <code>HEALTHY</code> to perform this action. Try again when the status is <code>HEALTHY</code>. </p>
        capo_cost_explorer.errors.data_unavailable_exception.DataUnavailableException: <p>The requested data is unavailable.</p>
        capo_cost_explorer.errors.invalid_next_token_exception.InvalidNextTokenException: <p>The pagination token is invalid. Try again without a pagination token.</p>
        capo_cost_explorer.errors.limit_exceeded_exception.LimitExceededException: <p>You made too many calls in a short period of time. Try again later.</p>
        capo_cost_explorer.errors.request_changed_exception.RequestChangedException: <p>Your request parameters changed between pages. Try again with the old parameters or without a pagination token.</p>
        capo_cost_explorer.errors.resource_not_found_exception.ResourceNotFoundException: <p> The specified ARN in the request doesn't exist. </p>
        capo_cost_explorer.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_cost_explorer.types.get_cost_and_usage_with_resources_request.GetCostAndUsageWithResourcesRequest]",
    ) -> AsyncOperationResponse[
        "capo_cost_explorer.types.get_cost_and_usage_with_resources_response.GetCostAndUsageWithResourcesResponse"
    ]:
        import capo_cost_explorer._operations.aws_insights_index_service.get_cost_and_usage_with_resources

        (
            output,
            http_response,
        ) = await capo_cost_explorer._operations.aws_insights_index_service.get_cost_and_usage_with_resources.async_get_cost_and_usage_with_resources(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("GetCostAndUsageWithResources")
    )
    input_: capo_cost_explorer.types.get_cost_and_usage_with_resources_request.GetCostAndUsageWithResourcesRequest = {
        "time_period": time_period,
        "granularity": granularity,
        "filter": filter,
    }
    if metrics is not None:
        input_["metrics"] = metrics
    if group_by is not None:
        input_["group_by"] = group_by
    if billing_view_arn is not None:
        input_["billing_view_arn"] = billing_view_arn
    if next_page_token is not None:
        input_["next_page_token"] = next_page_token

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output