How do I use Amazon Athena to query my Cost and Usage Reports to show total costs for each of my S3 buckets?

Last updated: 2019-09-30

I want to use Amazon Athena to query my Cost and Usage Report (CUR) and find out the total costs for each of my Amazon Simple Storage Service (Amazon S3) buckets. How do I do that?

Resolution

Before you get started, be sure that you've ingested your CUR into Amazon Athena.

Then, run the following query in Athena:

-- s3_by_bucket.sql
select
  "line_item_product_code",
  "line_item_usage_type",
  "line_item_resource_id",
  sum(case
    when "line_item_usage_type" like '%Byte%' then "line_item_usage_amount"/1024
    else "line_item_usage_amount"
  end) as "Usage",
  case
    when "line_item_usage_type" like '%Byte%' then 'TBs'
    else 'Requests'
  end as "Usage Units",
  sum("line_item_unblended_cost") as cost
from DATABASE.TABLE
where "line_item_product_code" = 'AmazonS3'
group by
  "line_item_product_code",
  "line_item_usage_type",
  "line_item_resource_id"
order by
  sum("line_item_unblended_cost") desc

Important: Replace DATABASE and TABLE with the name of your database and table.

This query retrieves the following:

  • Your total monthly S3 costs by usage type
  • The resource ID for your S3 buckets (bucket name)
  • The usage amount
  • The usage unit (TBs or requests)
  • The total cost (in USD)

For the current month, the output shows you your estimated costs.

You can add more columns and metadata to the output of this query. For example, you might add lineItem/UsageAccountId or product/region. For more information, see Cost and Usage Report Details.


Did this article help you?

Anything we could improve?


Need more help?