Skip to content

GetReservationsUtilization API returning all zero values, but i check in console it has data and values

0

I am trying to fetch the reservations utilization report from aws account using GetReservationsUtilizations API but when i check with that api i get all the values "0" in my lambda response,

import json
import boto3
from datetime import datetime

# Initialize the Cost Explorer client
ce_client = boto3.client('ce')

def lambda_handler(event, context):
    # Define the time period (modify the start and end dates as per your needs)
    start_date = '2025-01-01'
    end_date = '2025-02-01'

    # Parameters for the GetReservationUtilization API call (EC2 RI utilization)
    params = {
        'TimePeriod': {
            'Start': start_date,  # Start date of the period
            'End': end_date,      # End date of the period
        },
        'Granularity': 'DAILY',  # Granularity (DAILY, MONTHLY, or HOURLY)
        'GroupBy': [
            {
                'Type': 'DIMENSION',
                'Key': 'SUBSCRIPTION_ID'  # Grouping by subscription ID
            }
        ]
    }

    try:
        # Call the AWS Cost Explorer API to get EC2 Reserved Instance utilization data
        response = ce_client.get_reservation_utilization(**params)

        # Print or return the response data
        print("EC2 Reserved Instance Utilization Data:")
        print(json.dumps(response, indent=2))

        return {
            'statusCode': 200,
            'body': json.dumps(response)
        }

    except Exception as e:
        print(f"Error fetching EC2 Reserved Instance utilization data: {str(e)}")
        return {
            'statusCode': 500,
            'body': json.dumps({'error': str(e)})
        }

but when i see the console i can able to the all the cost values and details. this is the code i am using.

asked 2 years ago148 views

1 Answer
0

The issue you're experiencing with the GetReservationUtilization API returning zero values while the console shows data could be due to a few reasons:

  1. Date Range: Ensure that the date range you're querying in your code matches the period where you see data in the console. The API is very specific about dates, and the end date is exclusive. In your code, you're using future dates (2025), which won't have any data yet. Try using past dates where you know data exists.

  2. Time Period Formatting: Make sure your start and end dates are formatted correctly. The API expects dates in the format 'YYYY-MM-DD'.

  3. Granularity: You're using 'DAILY' granularity. If you're looking at a broader view in the console, try changing to 'MONTHLY' to match.

  4. Filters: The console might be applying different filters than your API call. Ensure that any filters you see in the console (like instance types or regions) are also applied in your API call if needed.

  5. Data Availability: There can sometimes be a delay between when data appears in the console and when it's available via the API. If you're looking at very recent data, it might not yet be accessible through the API.

  6. Permissions: Verify that the IAM role associated with your Lambda function has the necessary permissions to access Cost Explorer data.

To troubleshoot:

  • Use recent past dates where you know data exists.
  • Try removing the GroupBy parameter to get overall utilization first.
  • Double-check your account and region settings.
  • If possible, run a similar query directly in the AWS CLI to see if it produces the same result.

If the issue persists after these checks, you may need to contact AWS support for further assistance, as there could be an account-specific issue or a discrepancy between the console and API data.
Sources
GetReservationUtilization - AWS Billing and Cost Management
Different Total Cost in Cost Explorer vs API | AWS re:Post
RetrieveAndGenerate API response is giving empty list for retrievedReferences even when output is being generated and mapped to source docs. | AWS re:Post

answered 2 years ago

AWS
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.