May 2021
If you wish to provide feedback on this lab, there is an error, or you want to make a suggestion, please email: curquery@amazon.com
This lab will demonstrate the basic principles for constructing a basic CUR query. The skills you learn will help you construct your own CUR queries or modify queries already in use. While this lab cannot cover every use case for cost and usage analysis, many AWS services follow similar CUR logic and can be easily adapted.
Use the clipboard in the top right of the text boxes below to copy all of the text to be pasted.
This example shows how to query the usage and cost of an individual product in a specific date range. The results are summed for each individual account that incurred charges. The following sections will break the query down by section and explain the individual components.
SELECT
bill_payer_account_id,
line_item_usage_account_id,
line_item_line_item_description,
DATE_FORMAT((line_item_usage_start_date),'%Y-%m-%d') AS day_line_item_usage_start_date,
SUM(line_item_usage_amount) AS sum_line_item_usage_amount,
SUM(line_item_unblended_cost) AS sum_line_item_unblended_cost
FROM
${table_name}
WHERE
year = '2020' AND (month BETWEEN '7' AND '9' OR month BETWEEN '07' AND '09')
AND product_product_name LIKE '%${product_name}%'
GROUP BY
bill_payer_account_id,
line_item_usage_account_id,
line_item_line_item_description,
DATE_FORMAT((line_item_usage_start_date),'%Y-%m-%d'),
ORDER BY
sum_line_item_unblended_cost DESC;
Let’s take a look at each section of the query framework:
SELECT
bill_payer_account_id,
line_item_usage_account_id,
line_item_line_item_description,
DATE_FORMAT((line_item_usage_start_date),'%Y-%m-%d') AS day_line_item_usage_start_date,
SUM(line_item_unblended_cost) AS sum_line_item_unblended_cost
In this section, you select a number of fields from the CUR.
FROM
${table_name}
WHERE
year = '2020' AND (month BETWEEN '7' AND '9' OR month BETWEEN '07' AND '09')
AND product_product_name LIKE '%${product_name}%'
In this section, you set parameters to filter a subset of records in CUR. The first line of the WHERE clause sets a date range. See Filtering by Date in the CUR Query Library Help section for additional details.
GROUP BY
bill_payer_account_id,
line_item_usage_account_id,
line_item_line_item_description,
DATE_FORMAT((line_item_usage_start_date),'%Y-%m-%d'),
ORDER BY
sum_line_item_unblended_cost DESC;
In this section, you set grouping and ordering.
GROUP BY groups like records together. In this example, all records are grouped as follows:
Note: you cannot group by fields that use a SUM function.
ORDER BY sorts the list of returned records by cost in descending order (largest to smallest).
SELECT DISTINCT
product_product_name,
line_item_product_code
FROM
${table_name}
The above query will list each distinct product names and codes in CUR. You can use this list as a reference for filtering CUR queries by product name or product code. The product columns provide metadata about the product that incurred the expense, and the line item. The product columns are dynamic and their visibility in Cost and Usage Reports depends on the usage of product in the billing period.
When working with AWS Marketplace products it is best to use the column product_product_name as it contains a friendly name for the third party product. If you were to use line_item_product_code for AWS Marketplace products it will contain a unique ID.
product_product_name = CloudBeaver
line_item_product_code = 581uljmnj07lfrc1uqfd9skb2p
When working with Native AWS products is is best to use the column line_item_product_code. You could also add into your query a CASE statement to handle this:
CASE
WHEN ("bill_billing_entity" = 'AWS Marketplace' AND "line_item_line_item_type" NOT LIKE '%Discount%') THEN "product_product_name"
WHEN ("bill_billing_entity" = 'AWS') THEN "line_item_product_code" END "Service",
It is always best to confirm your query output against Cost Explorer before using it in a production setting.
Detailed descriptions of the all CUR fields can be found in the CUR Data Dictionary: https://docs.aws.amazon.com/cur/latest/userguide/data-dictionary.html
More information:
CUR queries are provided as is. We recommend validating your data by comparing it against your monthly bill and Cost Explorer prior to making any financial decisions. If you wish to provide feedback on these queries, there is an error, or you want to make a suggestion, please email: curquery@amazon.com