Chatwithcloud logo


AWS GenAI Tools

AWS Cost and Usage Analysis - Identify the Most Used Service

In this article, we'll review a JavaScript code extract that uses AWS SDK's CostExplorer object to retrieve the cost and usage data of your AWS services. The goal is to identify the service which incurs the highest cost during a specific time period.


const AWS = require('aws-sdk'); const costexplorer = new AWS.CostExplorer(); let params = { TimePeriod: { Start: '2023-10-01', End: '2023-11-01' }, Granularity: 'MONTHLY', Metrics: ['UnblendedCost'], GroupBy: [ { Type: 'DIMENSION', Key: 'SERVICE' } ] }; costexplorer.getCostAndUsage(params) .promise() .then(data => { let results = => => ({Service: group.Keys[0], Amount: group.Metrics.UnblendedCost.Amount}) ) .reduce((max, current) => (parseFloat(current.Amount) > parseFloat(max.Amount) ? current : max), {Service: '', Amount: '0'})) console.log(results); });

Detailed Code Explanation

The above code works in the following way:

  1. First, we initialize the CostExplorer object.
  2. We set our parameters for getCostAndUsage() method. We're specifying a monthly report for the time period from Oct 1, 2023, to Nov 1, 2023. Note: Adjust these dates as per your requirement.
  3. The metrics we're using is the UnblendedCost, which represents the cost of a service regardless of any discounts or credits.
  4. We use the GroupBy parameter to group costs by AWS services.
  5. We then call the getCostAndUsage method of Cost Explorer.
  6. When the Promise resolves, we transform the returned data into an array of highest spending services for each month of the specified range.

Expected Output Format

The program will print the result in the console in the following structure.

[ { "Service": "<Service-Name>", "Amount": "<Cost>" }, ... ]

Each object in the array corresponds to a month in the time range, providing a service name and the corresponding cost.

Considerations & Caveats

  1. The getCostAndUsage method can be throttled. To avoid hitting the rate limit, consider running the script with pauses or at different times.
  2. Ensure you understand AWS's cost and billing structure, as this script only provides a cost breakdown for 'UnblendedCost'.
  3. Cost data may take 24-48 hours to be processed and appear in AWS Cost Explorer. As a result, this script might not show the most recent cost data.
  4. Make sure the time range doesn't exceed one year, AWS Cost Explorer doesn't allow a bigger time range.

Required IAM permissions and example policy

Your IAM policy should have permissions for ce:GetCostAndUsage. Here's an example policy:

{ "Version": "2012-10-17", "Statement": [{ "Action": [ "ce:GetCostAndUsage" ], "Effect": "Allow", "Resource": "*" }] }


Q: What does UnblendedCost refer to? UnblendedCost is the unadjusted cost of a service before accounting for any discounts, savings, or credits.

Q: Why am I not getting the most recent cost data? The cost data in AWS Cost Explorer takes up to 24-48 hours to process. If you run this script within this timeframe, you might not see the most recent costs.

Q: How often should I run this script? This depends on your specific needs. For close monitoring of AWS costs, you might want to run this script daily. Be cautious of the getCostAndUsage API rate limit.

Q: Can this script check costs for a specific day? Yes, the script can be modified to do so by adjusting the 'Start' and 'End' dates in the 'TimePeriod' parameter to the same day. Note that the 'Granularity' parameter should also be set as 'DAILY'.

Related articles
Monitor and Notify When Approaching Service LimitsGet Current IAM Identity TypeScriptCalculate the Size of Each S3 Bucket and Find the One with the Most Data