Daily AWS Billing Alert

Rajat Goyal
Synaptic Engineering
3 min readJan 8, 2018

--

source:pexels

At Synaptic we believe in empowering developers to explore and use whatever technologies required, we have a separate developer AWS account for all our developers and people have unfettered freedom to experiment using our AWS resources. A few times the results can be surprising (people forget to shut down large instances after working). We wanted to prevent this and still not create unnecessary process around AWS allocation.

Read this so that you don’t get surprised by your monthly AWS bill again.

You can monitor your estimated AWS charges using Amazon CloudWatch. When you enable the monitoring of estimated charges for your AWS account, the total estimated charges for that month are calculated and sent several times daily to CloudWatch as metric data unfortunately AWS doesn’t have an api to get custom billing for a time-frame or set alert on something other than consolidated billing.

Using cloud-watch alerts you can set total monthly bill alert if the bill exceeds a certain amount as you would set this alert to the amount which you expect to be charged by the month’s end but this alert can get trigged in a few days and the damage is already done. The command mentioned at the end of the article will give you total bill for a period of of last 12 hours, so that you can identify if there are any unexpected charges before it’s too late.

This command uses AWS cli to get most recent total estimated bill, cloud-watch ‘EstimatedCharges’ metric gets one data point between 4 to 8 hours so we take the maximum of last 12 hours.

aws --region us-east-1 cloudwatch get-metric-statistics
--namespace "AWS/Billing"
--metric-name "EstimatedCharges"
--dimension "Name=Currency,Value=USD"
--start-time $(date +"%Y-%m-%dT%H:%M:00" --date="-12 hours") --end-time $(date +"%Y-%m-%dT%H:%M:00")
--statistic Maximum
--period 60
--output text | sort -r -k 3 | head -n 1 | cut -f 2

same can be repeated to get total bill between last 24 hour and last 12 hour

aws --region us-east-1 cloudwatch get-metric-statistics
--namespace "AWS/Billing"
--metric-name "EstimatedCharges"
--dimension "Name=Currency,Value=USD"
--start-time $(date +"%Y-%m-%dT%H:%M:00"
--date="-24 hours")
--end-time $(date +"%Y-%m-%dT%H:%M:00" --date="-12 hours")
--statistic Maximum
--period 60
--output text | sort -r -k 3 | head -n 1 | cut -f 2

You can get total bill in last 12 hours by subtracting the output of these two commands. I am using `awk` here to subtract maximum charge in last 12 hour and maximum estimated charge between last 24 hour and 12 hour. Lastly, You can use amazon SES to send the email with the total bill.

aws ses send-email \
--from "$from_email" \
--destination "ToAddresses=<$to_email>" \
--message "Subject={Data=Billing alert,Charset=utf8},Body={Text={Data=last 12 hour bill `echo $bill`,Charset=utf8}}" \
--region "us-east-1"

Everything in a single script:

You can set it as a cron so that you get a email every 12 hour about last 12 hour bill, or you can customize to send email only when the bill exceeds a certain amount.

Caveats:

  1. This is not exactly 12 hour bill, estimated charges metrics gets a point in approximately every 4 hours, so the bill amount can be of between 8–12 hours.
  2. AWS estimated charges metric has a bug which causes it to miss some data points, sometimes, which might cause inconsistencies. However, When you get a email with the bill that you didn’t expect you can always go and inspect why it happened.

Sources:

--

--