AWS Cost Explorer provides valuable insights into your AWS usage and spending. In this tutorial, we will walk through a Lambda function that generates a CSV report of AWS cost and usage data for a specified date range, focusing on services consumed by a specific AWS account.
Prerequisites
- AWS Lambda: Ensure you have permissions to access AWS Cost Explorer.
- AWS Cost Explorer API Access: Set up the necessary permissions for the
ce:GetCostAndUsage
action in your Lambda execution role.
The Lambda Function Code
Below is the complete Python script that we will deploy as an AWS Lambda function.
import json
import boto3
import csv
import io
client = boto3.client('ce')
def lambda_handler(event, context):
try:
print("Event:", event)
input_data = json.loads(event['input'])
start_date = input_data.get('startDate')
end_date = input_data.get('endDate')
print(f"Start Date: {start_date}, End Date: {end_date}")
response = client.get_cost_and_usage(
TimePeriod={
'Start': start_date,
'End': end_date
},
Granularity='DAILY',
Metrics=[
'AmortizedCost',
],
GroupBy=[
{
'Type': 'DIMENSION',
'Key': 'SERVICE'
},
],
Filter={
'Dimensions': {
'Key': 'LINKED_ACCOUNT',
'Values': ['Account-Id'] //provide account id
}
}
)
csv_output = io.StringIO()
csv_writer = csv.writer(csv_output)
header = ['Date', 'Service', 'AmortizedCost']
csv_writer.writerow(header)
results = response['ResultsByTime']
for result in results:
date = result['TimePeriod']['Start']
csv_writer.writerow([f"Date: {date}", "", ""])
for group in result['Groups']:
service = group['Keys'][0]
cost = group['Metrics']['AmortizedCost']['Amount']
csv_writer.writerow([date, service, cost])
csv_writer.writerow(["", "", ""])
csv_data = csv_output.getvalue()
print(csv_data)
return {
'statusCode': 200,
'body': csv_data,
'headers': {
'Content-Type': 'text/csv',
'Content-Disposition': 'attachment; filename="cost_usage_report.csv"'
}
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({"error": str(e)}),
'headers': {
'Content-Type': 'application/json'
};
}
Code Explanation
- Initialization:
- We import required modules:
json
,boto3
(AWS SDK for Python),csv
, andio
. - The
boto3.client('ce')
line initializes the AWS Cost Explorer client.
- Handling Events:
- The
lambda_handler
function receives an event containing thestartDate
andendDate
parameters, which specify the date range for the report. - We parse these parameters using
json.loads(event['input'])
and extract the required date values.
- Fetching Cost and Usage Data:
- The
get_cost_and_usage
method is called to retrieve the cost data, grouped by service, within the specified date range. - The
Filter
parameter is set to limit the data to a specific AWS account ID
- Generating the CSV Report:
- We create a CSV output using
io.StringIO()
and write headers withcsv_writer.writerow(header)
. - For each result in the response, we write the date, service, and amortized cost values to the CSV.
- Returning the Response:
- The CSV data is returned in the response body with appropriate content headers to make it downloadable.
Deploying the Lambda Function
To deploy this function:
- Go to the AWS Lambda Console.
- Create a new function and paste the above code into the code editor.
- Set up the necessary IAM role with permissions for Cost Explorer.
- Test the function with a sample event that includes
startDate
andendDate
.
Sample Input Event
Here’s a sample input to test your Lambda function:
{
"input": "{\"startDate\": \"2023-09-01\", \"endDate\": \"2023-09-30\"}"
}
Conclusion
This Lambda function allows you to automate the process of generating daily AWS cost reports by service, helping you gain insights into your AWS spending. By customizing the event parameters, you can easily generate reports for any date range and download them as CSV files.
Take control of your AWS spending! Implement this Lambda function to automate your cost reporting and gain valuable insights into your AWS usage. Don’t wait, start optimizing your cloud costs today by generating detailed CSV reports that keep your budget on track! -