Skip to content

Instantly share code, notes, and snippets.

@RajatGoyal
Last active September 7, 2018 18:21
Show Gist options
  • Save RajatGoyal/c03659391327cec917eb1341abebbbf1 to your computer and use it in GitHub Desktop.
Save RajatGoyal/c03659391327cec917eb1341abebbbf1 to your computer and use it in GitHub Desktop.
total estimated bill aws
#!/bin/sh
from_email="from@synaptic.com"
to_email="to@synaptic.com"
max_estimated_charge_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="-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)
max_estimated_charge_before_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)
bill=$(echo "$max_estimated_charge_12_hour $max_estimated_charge_before_last_12_hour" | awk '{print $1-$2}')
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment