Search code examples
amazon-web-servicesboto3aws-clicost-management

How to enable hourly granularity for cost and usage data in AWS?


I am trying to call get-cost-and-usage api of AWS to generate hourly report. But, i am getting an error around enabling an opt-in only feature.

aws ce get-cost-and-usage --time-period '{"Start": "2020-06-10T00:00:00Z", "End": "2020-06-15T00:00:00Z"}' --granularity 'HOURLY' --metrics "AmortizedCost" "BlendedCost" "NetAmortizedCost" "NetUnblendedCost" "NormalizedUsageAmount" "UnblendedCost" "UsageQuantity" --group-by Type=DIMENSION,Key=SERVICE

Error Message:

An error occurred (AccessDeniedException) when calling the GetCostAndUsage operation: Hourly data granularity is an opt-in only feature. You can be enable this feature from the PAYER account’s Cost Explorer Settings page.

I don't see any settings page to enable this feature. Is there any way to enable this feature, via console or cli?


Solution

  • It is mentioned in many documentation links about.

    Hourly data granularity is an opt-in only feature.
    

    And when we execute the CE API, it also throws the error.

    An error occurred (AccessDeniedException) when calling the GetCostAndUsage operation: Hourly data granularity is an opt-in only feature. You can be enable this feature from the PAYER account’s Cost Explorer Settings page.
    

    But no screenshot or detailed information is provided any where. I am attaching the screenshot, How to enable "Hourly and Resource Level Data" e.g opt-in for Hourly data granularity.

    I am using consolidated Billing account, so this setting is needed in main Payer's account.

    enter image description here

    Few things more to consider about this settings:

    Allow all accounts to access hourly and resource-level information for the past 14 days. Resource-level information is only available for your Amazon EC2 service usage.

    Please note that it can take up to 24 hours for your data to become available, and a cost of $0.01 per 1,000 UsageRecords-month will be incurred as a result of storing and accessing your hourly- and resource-level information. A UsageRecord is defined as one line of usage. For example, one EC2 instance running for 24 hours will generate 24 distinct UsageRecords at the hourly granularity.

    After this settings, you can run CE API Calls. e.g

    aws ce get-cost-and-usage --time-period Start="2020-07-07T10:00:00Z",End="2020-07-07T11:00:00Z" --granularity HOURLY --metrics "BlendedCost" --group-by Type=DIMENSION,Key=SERVICE Type=TAG,Key=Environment
    

    One thing to note is that, in case of HOURLY, the time format must be in yyyy-MM-ddThh:mm:ssZ format.