Skip to content

Possible to query boto3's "get_cost_and_usage_with_resources" for more than 14 days?

0

Is there any way to query more than 14 days including the resource_id dimension?

    import json
    from datetime import datetime, timedelta, timezone

    import boto3

    UTC_TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
    DAYS=14
    GRANULARITY="MONTHLY"

    def get_start_end_date():
        end_date = datetime.now(timezone.utc) 
        start_date = end_date - timedelta(days=DAYS)
        return start_date.date(), end_date.date()

    start, end = get_start_end_date()
    response = ce_client.get_cost_and_usage_with_resources(
        TimePeriod={
            "Start": start.strftime(UTC_TIMESTAMP_FORMAT),
            "End": end.strftime(UTC_TIMESTAMP_FORMAT),
        },
        Granularity=GRANULARITY,
        Filter={
            "Not": {
                "Dimensions": {
                    "Key": "RESOURCE_ID",
                    "Values": [
                        "Credit"
                    ],
                }
            }
        },
        Metrics=["UsageQuantity", "BlendedCost"],
        GroupBy=[
            {
                "Type": "DIMENSION",
                "Key": "RESOURCE_ID",
            },
        ],
    )

No matter what GRANULARITY is set to, setting DAYS > 14 leads to

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the GetCostAndUsageWithResources operation: start date is too old for hourly, the max supported days for hourly granularity is 14 days

Did anyone manage to query resource level data somehow for more than 14 days?

I found a ticket from 2020 with the same issue: https://github.com/boto/boto3/issues/2369

asked 2 years ago619 views

1 Answer
2

Cost Explorer, that provides the API you are trying to use only holds resource data for 14 days.

In order to get resource ID information for more than 14 days, the better method is to configure Cost and Usage Reports (CUR) to be delivered to an S3 bucket and query the data there. This data will remain (unless you delete it or setup an S3 lifecycle policy) which would allow for multi-year review. The data in S3 can be queried by Athena or a Business Intelligence (BI) tool such as Quicksight or a 3rd party provider.

AWS
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.