Created
September 9, 2020 20:56
-
-
Save franzinc/f9b4c92307998a7940cf4dc1f0b873cc to your computer and use it in GitHub Desktop.
Print an estimate of your current AWS bill, with the incremental change in the last 24 hours
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
# Print the current bill for each AWS account, with a total at the end | |
# for all accounts. | |
# ideas from and thanks to: | |
# https://medium.com/synaptic-tech/daily-aws-billing-alert-4ea541475ff8 | |
set -eu | |
# Change "..." to contain the profile names of your AWS accounts. | |
identities="..." | |
function regions { | |
aws --profile $1 ec2 describe-regions --output text | cut -f4 | |
} | |
function charges_current { | |
local profile=$1 | |
local region=$2 | |
aws --profile $profile --region $region 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 | tail -n +2 | sort -r -k 3 | head -n 1 | cut -f 2 | |
} | |
function charges_dayago { | |
local profile=$1 | |
local region=$2 | |
aws --profile $profile --region $region 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 | tail -n +2 | sort -r -k 3 | head -n 1 | cut -f 2 | |
} | |
{ | |
grand_total=("0.0") | |
grand24_total=("0.0") | |
for id in $identities; do | |
current_by_id=("0.0") | |
dayago_by_id=("0.0") | |
for r in $(regions $id); do | |
current=$(charges_current $id $r) | |
dayago=$(charges_dayago $id $r) | |
# if empty respose then continue to next region | |
[ "$current" ] || continue | |
current_by_id+=(" + " "$current") | |
dayago_by_id+=(" + " "$dayago") | |
done | |
current_total=$(echo ${current_by_id[@]} | bc -l) | |
dayago_total=$(echo ${dayago_by_id[@]} | bc -l) | |
diff24=$(echo $current_total - $dayago_total | bc -l) | |
cat <<EOF | |
Current bill for account ${id}: \$$current_total (last 24hrs: \$$diff24) | |
EOF | |
grand_total+=(" + " "$current_total") | |
grand24_total+=(" + " "$dayago_total") | |
done | |
gt=$(echo ${grand_total[@]} | bc -l) | |
gt24=$(echo ${grand24_total[@]} | bc -l) | |
diff24=$(echo $gt - $gt24 | bc -l) | |
cat <<EOF | |
Total for all accounts: \$$gt (last 24hrs: \$$diff24) | |
EOF | |
exit 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment