Getting Started with the AWS Cost Explorer API
Published: 2023-03-14 15:58:21

Image Source 

## What Is AWS Cost Explorer?

AWS Cost Explorer is a [cost management tool](https://spot.io/resources/aws-cost-optimization/8-tools-and-tips-to-reduce-your-cloud-costs/) provided by Amazon Web Services (AWS). AWS Cost Explorer lets you analyze and view your cloud usage and [AWS costs](https://bluexp.netapp.com/blog/3-ways-to-save-big-and-10-price-variations-to-know-aws-cvo-blg) through several helpful features. It provides a main graph that displays costs and usage, preconfigured views with at-a-glance information, allows customizing views, and can produce reports.

Cost Explorer can display data from the last 12 months to help forecast how you're spending for the following 12 months. It can recommend suitable Reserved Instances for your use case and help you identify areas that require further inquiry and discover cost trends.

AWS offers a Cost Explorer user interface that is free of charge. To access data programmatically through the Cost Explorer API, you must pay a fee of $0.01 for each paginated API request. Note it is not possible to disable Cost Explorer once you enable it.

Cost explorer is very useful for assessing costs when [migrating workloads to AWS](https://bluexp.netapp.com/blog/aws-migration-5-key-challenges-and-solutions).
 

## What Is the AWS Cost Explorer API?

AWS offers a Cost Explorer API that enables programmatically querying cost and usage data, such as total daily usage, total monthly costs, and even the number of daily write operations for database tables in a production environment.

AWS recommends using one of the official SDKs for the programming languages the API supports. These SDKs can simplify the process of signing requests, save time, and integrate with development environments to provide easy access to commands.

## Using the AWS Cost Explorer API

To use the AWS Cost Explorer API, you will need to have an AWS account and have the appropriate permissions to access the API. Here are the basic steps for using the AWS Cost Explorer API:

1. **Create an IAM user with the appropriate permissions**: Before using the Cost Explorer API, you will need to create an IAM user with the appropriate permissions to access the API. You will need to create an IAM policy that allows the user to access the Cost Explorer API, and attach the policy to the user.

2. **Enable Cost Explorer**: To use the Cost Explorer API, you will need to enable the Cost Explorer service for your AWS account. You can do this by going to the Cost Explorer console and selecting "Enable Cost Explorer."

3. **Set up authentication**: To authenticate with the Cost Explorer API, you will need to generate an access key and secret key for the IAM user you created in step 1. You can do this in the IAM console by selecting the user and clicking "Create Access Key."

4. **Install an SDK or use the AWS CLI**: To access the Cost Explorer API, you can use an SDK (software development kit) for your programming language, or you can use the AWS CLI (command line interface). The SDKs provide a more programmatic way to access the API, while the AWS CLI provides a command-line interface.

5. **Use the API**: Once you have set up authentication and chosen a method for accessing the API, you can start using the API to retrieve cost and usage data. The API provides a wide range of options for querying and retrieving data, including time ranges, accounts, services, and other dimensions.

Some example use cases for the AWS Cost Explorer API include:

- Creating custom reports and visualizations of AWS costs and usage.
- Generating alerts and notifications based on changes in AWS costs and usage.
- Automating the process of generating cost and usage reports on a regular basis.
- Analyzing cost and usage data to identify areas for cost optimization and cost reduction.

## AWS Cost Explorer API Examples

Here are a few examples of using the AWS Cost Explorer API:

## Retrieve cost and usage data for a specific time range

To retrieve cost and usage data for a specific time range, you can use the GetCostAndUsage API operation. Here's an example of using the AWS SDK for Python to retrieve cost and usage data for the month of January 2022. See the code in [Google Colab](https://colab.research.google.com/drive/1YWkwxeHcrSCRm1Ky5jL46Ky7EgLn1vXV?usp=sharing).

```
import boto3

client = boto3.client('ce')

response = client.get_cost_and_usage(
    TimePeriod={
        'Start': '2023-02-20',
        'End': '2023-02-22'
    },
    Granularity='DAILY',
    Metrics=['BlendedCost'],
    GroupBy=[
        {
            'Type': 'DIMENSION',
            'Key': 'SERVICE'
        }
    ]
)

print(response)

The output will look something like this:

python3 test.py
{'GroupDefinitions': [{'Type': 'DIMENSION', 'Key': 'SERVICE'}], 'ResultsByTime': [{'TimePeriod': {'Start': '2023-02-20', 'End': '2023-02-21'}, 'Total': {}, 'Groups': [{'Keys': ['AWS Data Transfer'], 'Metrics': {'BlendedCost': {'Amount': '-0.0004244712', 'Unit': 'USD'}}}, {'Keys': ['EC2 - Other'], 'Metrics': {'BlendedCost': {'Amount': '0.0003690552', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic Compute Cloud - Compute'], 'Metrics': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}}, {'Keys': ['Amazon Relational Database Service'], 'Metrics': {'BlendedCost': {'Amount': '-0.0000000002', 'Unit': 'USD'}}}, {'Keys': ['Amazon Route 53'], 'Metrics': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}}, {'Keys': ['Amazon Simple Storage Service'], 'Metrics': {'BlendedCost': {'Amount': '-0.0000000001', 'Unit': 'USD'}}}, {'Keys': ['AmazonCloudWatch'], 'Metrics': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}}], 'Estimated': True}, {'TimePeriod': {'Start': '2023-02-21', 'End': '2023-02-22'}, 'Total': {}, 'Groups': [{'Keys': ['AWS Data Transfer'], 'Metrics': {'BlendedCost': {'Amount': '-0.0000712028', 'Unit': 'USD'}}}, {'Keys': ['EC2 - Other'], 'Metrics': {'BlendedCost': {'Amount': '0.0000217397', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic Compute Cloud - Compute'], 'Metrics': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}}, {'Keys': ['Amazon Relational Database Service'], 'Metrics': {'BlendedCost': {'Amount': '-0.0000000002', 'Unit': 'USD'}}}, {'Keys': ['Amazon Route 53'], 'Metrics': {'BlendedCost': {'Amount': '-0', 'Unit': 'USD'}}}, {'Keys': ['Amazon Simple Storage Service'], 'Metrics': {'BlendedCost': {'Amount': '-0.0000000001', 'Unit': 'USD'}}}, {'Keys': ['AmazonCloudWatch'], 'Metrics': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}}], 'Estimated': True}], 'DimensionValueAttributes': [], 'ResponseMetadata': {'RequestId': 'bcf84326-abfc-482e-acec-271e5f0e2062', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 25 Feb 2023 15:52:59 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '1666', 'connection': 'keep-alive', 'x-amzn-requestid': 'bcf84326-abfc-482e-acec-271e5f0e2062', 'cache-control': 'no-cache'}, 'RetryAttempts': 0}}

```
**Note**: Please use dates within a 12 month range, otherwise you will receive an error.


This code retrieves the daily blended cost for each AWS service used during January 2022, using the group-by dimension SERVICE.

**Retrieve recommendations for cost savings**
To retrieve recommendations for cost savings, you can use the GetCostForecast API operation. Here's an example of using the AWS SDK for Java to retrieve recommendations for cost savings. See the code in [Google Colab](https://colab.research.google.com/drive/1YWkwxeHcrSCRm1Ky5jL46Ky7EgLn1vXV?usp=sharing).

```
import com.amazonaws.services.costexplorer.AWSCostExplorer;
import com.amazonaws.services.costexplorer.model.DateInterval;
import com.amazonaws.services.costexplorer.AWSCostExplorerClientBuilder;
import com.amazonaws.services.costexplorer.model.GetCostForecastRequest;
import com.amazonaws.services.costexplorer.model.GetCostForecastResult;
import com.amazonaws.services.costexplorer.model.ForecastResult;
import com.amazonaws.services.costexplorer.model.DimensionValues;
import java.util.*;  

class test {

  public static void main(String args[]) {

AWSCostExplorer client = AWSCostExplorerClientBuilder.defaultClient();

GetCostForecastRequest request = new GetCostForecastRequest()
    .withTimePeriod(new DateInterval().withStart("2023-02-20").withEnd("2023-02-22"))
    .withMetric("BLENDED_COST")
    .withGranularity("DAILY")
    .withFilter(new Expression().withDimensions(
        new DimensionValues().withKey("SERVICE").withValues("AmazonEC2")
    ));

GetCostForecastResult result = client.getCostForecast(request);

List<ForecastResult> forecasts = result.getForecastResultsByTime();
for (ForecastResult forecast : forecasts) {
    System.out.println(forecast.getMeanValue());
}
   }
}

```
These are just a few examples of using the AWS Cost Explorer API. The API provides a wide range of options for querying and retrieving cost and usage data, and can be used to create custom reports, automate cost optimization, and more.

**Best Practices for the AWS Cost Explorer API**
_Configuring Access to the Cost Explorer API_
To ensure the security and integrity of your AWS resources and data, it's important to use AWS Identity and Access Management (IAM) to control access to the Cost Explorer API. IAM allows you to create and manage users, groups, and roles that have specific permissions to access the Cost Explorer API actions and resources. 

When creating IAM policies for the Cost Explorer API, you should grant permissions only to the specific actions and resources that are required for your users or applications, to limit the risk of unauthorized access or misuse. Enabling AWS CloudTrail can also help you track and audit all API activity and changes to IAM policies, so you can identify any suspicious or unauthorized activity.

_Querying the Cost Explorer API_
To optimize your use of the Cost Explorer API, you should use filters and grouping parameters to limit the amount of data returned by the API to only what you need. For example, you can use the "time period" parameter to specify a specific date range, or use the "group by" parameter to group costs by service, account, or other dimensions. 

This can help you reduce the amount of data that your application needs to process, which can improve performance and reduce your API costs. Additionally, you can use the paging feature of the API to retrieve data in smaller chunks, which can help you avoid exceeding API request quotas and reduce the amount of memory and processing required by your application. Finally, you can cache frequently accessed data to reduce the number of API calls and improve performance.

_Optimizing Your Cost Explorer API Costs_
To minimize your costs for using the Cost Explorer API, you can use the GetCostAndUsage and GetCostForecast API actions instead of the GetDimensionValues action, as the former can return more data per API call, reducing the number of calls required. You can also use compressed responses to reduce the size of the data returned by the API and lower your data transfer costs. 

Additionally, you can use AWS Lambda or other serverless compute services to execute your Cost Explorer API queries, as this can help you reduce the amount of compute resources you need to provision, which can lower your costs. Serverless compute services can scale your application automatically based on demand to help improve performance and reduce costs.

**Conclusion**
In conclusion, the AWS Cost Explorer API is a powerful tool for managing your AWS spending and optimizing your cloud costs. By following best practices for configuring access to the API, querying the API, and optimizing your API costs, you can improve the performance and cost-effectiveness of your applications and services in the cloud.