Query on Obtaining Resource-Based Usage Cost Breakdown without Incurring Additional Charges

0

Hello AWS Community,

I hope this message finds you well. I am currently facing a challenge in obtaining a detailed cost breakdown based on individual resources without incurring additional charges related to granularity activation. I have explored the AWS Cost Explorer API and AWS CLI commands, but haven't found a solution that meets my specific requirements.

The command I have tried is as follows:

aws ce get-cost-and-usage --time-period Start=2023-12-10,End=2023-12-11 --granularity DAILY --metrics "BlendedCost" "UnblendedCost" "UsageQuantity" --group-by Type=DIMENSION,Key=SERVICE Type=TAG,Key=Env --region us-east-2 --filter file://filters.json

This command returns cost usage based on services, and the accuracy is not satisfactory for my specific use case.

Additionally, I attempted to use the get-cost-and-usage-with-resources command:

aws ce get-cost-and-usage-with-resources --time-period Start=2023-12-01,End=2023-12-11 --granularity HOURLY --metrics "BlendedCost" "UnblendedCost" "UsageQuantity" --group-by Type=DIMENSION,Key=SERVICE Type=TAG,Key=Env --filter file://filters.json

Unfortunately, this command requires activating granularity, which I wish to avoid to prevent incurring unnecessary charges.

My goal is to access the usage cost breakdown based on individual resources without activating additional granularity that may lead to extra expenses. I am seeking guidance on AWS CLI or API commands that can help achieve this objective without incurring additional costs related to granularity activation.

Any insights or alternative approaches from the community would be highly appreciated.

2 Answers
1
Accepted Answer

Hello,

Did you consider Cost and Usage Report (CUR)? This is the most detailed and granular data you can get about AWS usage. Enabling CUR won't create any additional cost, data is stored in S3, and you'll have to pay for the amount of data stored there. https://docs.aws.amazon.com/cur/latest/userguide/cur-create.html There are several ways to access data, from SQL queries leveraging Athena, to dashboards built on AWS services (QuickSight, Managed Grafana) or 3rd party tools. https://aws.amazon.com/blogs/mt/visualize-and-gain-insights-into-your-aws-cost-and-usage-with-amazon-managed-grafana/

Cheers.

AWS
answered 5 months ago
profile picture
EXPERT
reviewed a month ago
  • Hello,

    Thank you for your suggestion. I have considered Cost and Usage Reports (CUR) previously, but I'm specifically looking for an API, SDK, or AWS CLI solution to automate the extraction of detailed cost breakdowns based on individual resources. I want to avoid any clickable actions or manual steps to ensure a seamless integration into my existing workflow.

    If there are any specific AWS CLI commands or API calls that can provide resource-based cost breakdowns without the need for granularity activation, I would greatly appreciate the guidance.

    Thank you for your continued assistance.

  • Thank you for the response; the provided solution works seamlessly!

1

Hello Again,

You don't have to use Athena console to query data. There are several alternatives, SDKs (EG boto3 for Python), JDBC access, AWS CLI, to name a few. Here an example using AWS CLI

  • Run your query (in this case I want to know the most expensive CloudWatch resource in my organization for November 2023
aws athena start-query-execution \
    --query-string "SELECT line_item_resource_id, SUM(line_item_unblended_cost) FROM customer_all WHERE year = '2023'     AND month = '11'     AND line_item_product_code = 'AmazonCloudWatch' GROUP BY 1  ORDER BY 2 DESC" \
    --work-group "primary" \
    --query-execution-context Database=customer_cur_data,Catalog=AwsDataCatalog

The result is the execution ID

{ "QueryExecutionId": "b00ea88a-ae00-4020-81cf-3310bd9f9556" }

You can then access the result from the S3 (defined in Athena as query result location)

`aws s3 cp s3://aws-athena-query-results-11111111111-us-east-1/b11ea11a-ae11-1111-11cf-1110bd1f1111.csv .`

A couple of lines from the cvs file:

 line_item_resource_id	_col1
arn:aws:logs:ap-southeast-2:111111111111:log-group:/prefix/name1	2454.3543
arn:aws:logs:ap-southeast-2:111111111111:log-group:/prefix/name2	2193.3111

Some references: https://awscli.amazonaws.com/v2/documentation/api/latest/reference/athena/start-query-execution.html https://docs.aws.amazon.com/athena/latest/ug/querying.html#query-results-specify-location

AWS
answered 5 months 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.

Guidelines for Answering Questions