Skip to content

Analyzing On-Demand RDS Usage for RI Planning with Athena (Legacy CUR)

5 minute read
Content level: Intermediate
0

Use this script only for usage analysis and understanding your on-demand patterns. Use AWS RI Recommendations for actual RI purchases, since those consider normalized units and size flexibility.

Authored by: Abdul Majid Mohammed

Note on Normalized Units (Read First)

This query helps you understand on-demand RDS and Aurora usage, including daily instance counts and monthly averages. However, the AWS Cost Management → Reservations → Recommendations page provides normalized units when generating RI purchase guidance. This aligns with the RDS Reserved Instance size-flexibility model:

  • Some RDS instance families support size flexibility
  • AWS converts usage into normalized units to compare different instance sizes
  • Recommendations optimize RI coverage across an entire instance family

Why this query is useful

This query extracts only on-demand usage, converts usage hours to daily instance counts, and computes an average monthly uncovered instance requirement. That value helps you validate RI coverage gaps.

For RI planning, it is essential to isolate on-demand instance-hours—the hours not already covered by RIs.

  • Usage = on-demand
  • DiscountedUsage = RI-covered

Athena Query (works with Legacy CUR)

Notes:

  • Change the year_value and month_value in the query, as needed.
  • Change the <your_cur_table> in the query with your table name.
WITH params AS (
  SELECT
    year_value,
    month_value,
    date_diff(
      'day',
      date_parse(year_value || '-' || month_value || '-01', '%Y-%m-%d'),
      date_add(
        'month',
        1,
        date_parse(year_value || '-' || month_value || '-01', '%Y-%m-%d')
      )
    ) AS days_in_month
  FROM (
    SELECT
      '2025' AS year_value,
      '10'   AS month_value   -- change month (zero-padded)
  )
),

base AS (
  SELECT
    bill_payer_account_id      AS payer_account_id,
    line_item_usage_account_id AS linked_account,

    -- Uncomment for per-resource analysis:
    -- line_item_resource_id      AS resource_id,

    TRIM(split_part(line_item_usage_type, ':', 2)) AS instance_size,
    COALESCE(product_region_code, product_region) AS region,
    COALESCE(NULLIF(product_database_engine, ''), product_engine, 'Unknown') AS engine,
    CAST(line_item_usage_start_date AS DATE) AS usage_day,
    SUM(line_item_usage_amount) AS usage_hours
  FROM <your_cur_table>
  CROSS JOIN params p
  WHERE line_item_product_code = 'AmazonRDS'
    AND line_item_line_item_type = 'Usage'      -- on-demand only
    AND (
          line_item_usage_type LIKE '%InstanceUsage:db.%'
       OR line_item_usage_type LIKE '%Multi-AZUsage:db.%'
        )
    AND year  = p.year_value
    AND month = p.month_value
  GROUP BY
    bill_payer_account_id,
    line_item_usage_account_id,
    -- line_item_resource_id,
    TRIM(split_part(line_item_usage_type, ':', 2)),
    COALESCE(product_region_code, product_region),
    COALESCE(NULLIF(product_database_engine, ''), product_engine, 'Unknown'),
    CAST(line_item_usage_start_date AS DATE)
)

SELECT
  b.payer_account_id,
  b.linked_account,
  b.region,
  b.engine,
  b.instance_size,
  -- b.resource_id,

  SUM(CASE WHEN day(usage_day)= 1 THEN usage_hours/24.0 ELSE 0 END) AS day01,
  SUM(CASE WHEN day(usage_day)= 2 THEN usage_hours/24.0 ELSE 0 END) AS day02,
  SUM(CASE WHEN day(usage_day)= 3 THEN usage_hours/24.0 ELSE 0 END) AS day03,
  SUM(CASE WHEN day(usage_day)= 4 THEN usage_hours/24.0 ELSE 0 END) AS day04,
  SUM(CASE WHEN day(usage_day)= 5 THEN usage_hours/24.0 ELSE 0 END) AS day05,
  SUM(CASE WHEN day(usage_day)= 6 THEN usage_hours/24.0 ELSE 0 END) AS day06,
  SUM(CASE WHEN day(usage_day)= 7 THEN usage_hours/24.0 ELSE 0 END) AS day07,
  SUM(CASE WHEN day(usage_day)= 8 THEN usage_hours/24.0 ELSE 0 END) AS day08,
  SUM(CASE WHEN day(usage_day)= 9 THEN usage_hours/24.0 ELSE 0 END) AS day09,
  SUM(CASE WHEN day(usage_day)=10 THEN usage_hours/24.0 ELSE 0 END) AS day10,
  SUM(CASE WHEN day(usage_day)=11 THEN usage_hours/24.0 ELSE 0 END) AS day11,
  SUM(CASE WHEN day(usage_day)=12 THEN usage_hours/24.0 ELSE 0 END) AS day12,
  SUM(CASE WHEN day(usage_day)=13 THEN usage_hours/24.0 ELSE 0 END) AS day13,
  SUM(CASE WHEN day(usage_day)=14 THEN usage_hours/24.0 ELSE 0 END) AS day14,
  SUM(CASE WHEN day(usage_day)=15 THEN usage_hours/24.0 ELSE 0 END) AS day15,
  SUM(CASE WHEN day(usage_day)=16 THEN usage_hours/24.0 ELSE 0 END) AS day16,
  SUM(CASE WHEN day(usage_day)=17 THEN usage_hours/24.0 ELSE 0 END) AS day17,
  SUM(CASE WHEN day(usage_day)=18 THEN usage_hours/24.0 ELSE 0 END) AS day18,
  SUM(CASE WHEN day(usage_day)=19 THEN usage_hours/24.0 ELSE 0 END) AS day19,
  SUM(CASE WHEN day(usage_day)=20 THEN usage_hours/24.0 ELSE 0 END) AS day20,
  SUM(CASE WHEN day(usage_day)=21 THEN usage_hours/24.0 ELSE 0 END) AS day21,
  SUM(CASE WHEN day(usage_day)=22 THEN usage_hours/24.0 ELSE 0 END) AS day22,
  SUM(CASE WHEN day(usage_day)=23 THEN usage_hours/24.0 ELSE 0 END) AS day23,
  SUM(CASE WHEN day(usage_day)=24 THEN usage_hours/24.0 ELSE 0 END) AS day24,
  SUM(CASE WHEN day(usage_day)=25 THEN usage_hours/24.0 ELSE 0 END) AS day25,
  SUM(CASE WHEN day(usage_day)=26 THEN usage_hours/24.0 ELSE 0 END) AS day26,
  SUM(CASE WHEN day(usage_day)=27 THEN usage_hours/24.0 ELSE 0 END) AS day27,
  SUM(CASE WHEN day(usage_day)=28 THEN usage_hours/24.0 ELSE 0 END) AS day28,
  SUM(CASE WHEN day(usage_day)=29 THEN usage_hours/24.0 ELSE 0 END) AS day29,
  SUM(CASE WHEN day(usage_day)=30 THEN usage_hours/24.0 ELSE 0 END) AS day30,
  SUM(CASE WHEN day(usage_day)=31 THEN usage_hours/24.0 ELSE 0 END) AS day31,

  ROUND(
    SUM(usage_hours) / (24.0 * MAX(p.days_in_month)),
    2
  ) AS Avg_on_demand_instance_count_for_RIs

FROM base b
CROSS JOIN params p
GROUP BY
  b.payer_account_id,
  b.linked_account,
  b.region,
  b.engine,
  b.instance_size
ORDER BY
  b.payer_account_id,
  b.linked_account,
  b.region,
  b.engine,
  b.instance_size;

If you want to view the instance count by 'resource_id'

Enable it by uncommenting two lines in both the SELECT and GROUP BY sections. Otherwise, leave the resource_id commented out.


Conclusion

Use this query to understand raw on-demand RDS and Aurora usage, visualize daily behavior, and identify persistent uncovered demand. For actual RI purchasing, rely on AWS RI Recommendations, on AWS Cost Management → Reservations → Recommendations page, which evaluate normalized units and offer better flexibility across instance sizes.