Automating AWS Billing Reports and Email Notifications with Shell Scripting
1. Introduction: This documentation outlines the process of automating the retrieval of AWS billing details and sending daily email reports to a manager using a shell script. The script utilizes AWS CLI for querying billing data, jq
for JSON parsing, and crontab
for scheduling the script to run daily.
2. Requirements: Ensure the following requirements are met:
AWS CLI is installed and configured with necessary permissions.
jq
JSON processor is installed on the system.AWS SES is set up and verified to send emails from.
crontab
is available on your system.
3. Installation ofjq
:
# Update package index
sudo apt-get update
# Install jq
sudo apt-get install jq
For other Linux distributions or macOS, you can use their respective package managers to install jq
.
4. Script Overview and Configuration:
The provided shell script retrieves AWS billing details for the current day and formats them into an email report. Below is an overview of the script's functionality:
Retrieves the current date and the next day's date.
Queries AWS Cost Explorer API to fetch cost and usage data for the current day.
Parses the JSON output to extract the total unblended cost.
Formats the billing report including the date and the total cost.
Sends the report via email using AWS SES.
#!/bin/bash
CURRENT_DATE=$(date +"%Y-%m-%d")
NEXT_DATE=$(date -d "${CURRENT_DATE} + 1 day" +"%Y-%m-%d")
#API to get the Unblended Cost from the aws account
COST=$(aws ce get-cost-and-usage \
--time-period Start=${CURRENT_DATE},End=${NEXT_DATE} \
--granularity DAILY \
--metrics "UnblendedCost")
#Printing the aws cost
echo -e "printing cost $COST"
#Retriving the Cost value from the Total Cost Json
ACTUAL_AWS_COST=$(echo "$COST" | jq '.ResultsByTime[0].Total.UnblendedCost.Amount')
#Printing the Actual Cost
echo -e "Printing Actual cost $ACTUAL_AWS_COST"
#Formating the Data for Sending to the email body
OUTPUT="AWS Daily Billing Report\n\nDate: $CURRENT_DATE\nEstimated Cost: $ACTUAL_AWS_COST USD"
#Printing the Output
echo -e "$OUTPUT"
# Send email using AWS SES
aws ses send-email \
--from "youremail@gmail.com" \
--to "youremail@gmail.com" \
--subject "AWS Daily Billing Report" \
--text "$OUTPUT"
5. Execution:
Save the script to a file (e.g.,
aws_billing_
report.sh
).Make the script executable:
chmod 744 aws_billing_
report.sh
.
6. Configuringcrontab
: To schedule the script to run automatically every day at 5 PM, follow these steps:
- Open your terminal and type:
crontab -e
Add the following line to the crontab file:
0 17 * * * /path/to/aws_billing_report.sh
This cron job runs the script /path/to/aws_billing_
report.sh
every day at 5 PM. Adjust the path to your script accordingly.
6. OutPut: Output when i run script manually
7. Email: Email
8. Conclusion: By following this documentation, you can automate the process of retrieving AWS billing details and sending daily reports via email. This script enhances efficiency and facilitates better monitoring of AWS costs.
Subscribe to my newsletter
Read articles from Sravya Yakkati directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by