Skip to content

Instantly share code, notes, and snippets.

@zobkiw
Created April 10, 2021 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zobkiw/7acd97b45feeacea57beb3e53ac35f31 to your computer and use it in GitHub Desktop.
Save zobkiw/7acd97b45feeacea57beb3e53ac35f31 to your computer and use it in GitHub Desktop.
aws_billing - assuming a proper install and configuration of the aws cli and jq, call this script with the name of a configured profile (or default) and it will print out a summary of your bill.
#!/bin/bash
profile=$1 # aws config profile name or default ie: ./aws_billing.sh default
if [ -z $1 ]; then
echo "profile must be the first and only parameter...exiting."
exit 0
fi
echo "Gathering pricing information for DAY BEFORE YESTERDAY..."
day_before_yesterday_date=$(date -u -v-2d +%Y-%m-%d)
day_before_yesterday_cost=$(aws ce get-cost-and-usage --time-period Start=$(date -u -v-2d +%Y-%m-%d),End=$(date -u -v-1d +%Y-%m-%d) --granularity MONTHLY --metrics UnblendedCost --profile "${profile}" | jq '.ResultsByTime[0].Total.UnblendedCost.Amount' | xargs printf "%'0.2f\n")
echo "Gathering pricing information for YESTERDAY..."
yesterday_date=$(date -u -v-1d +%Y-%m-%d)
yesterday_cost=$(aws ce get-cost-and-usage --time-period Start=$(date -u -v-1d +%Y-%m-%d),End=$(date -u +%Y-%m-%d) --granularity MONTHLY --metrics UnblendedCost --profile "${profile}" | jq '.ResultsByTime[0].Total.UnblendedCost.Amount' | xargs printf "%'0.2f\n")
echo "Gathering pricing information for TODAY..."
today_date=$(date -u +%Y-%m-%d)
today_cost=$(aws ce get-cost-and-usage --time-period Start=$(date -u +%Y-%m-%d),End=$(date -u -v+1d +%Y-%m-%d) --granularity MONTHLY --metrics UnblendedCost --profile "${profile}" | jq '.ResultsByTime[0].Total.UnblendedCost.Amount' | xargs printf "%'0.2f\n")
echo "Gathering pricing information for MONTH TO DATE..."
month_to_date_cost=$(aws ce get-cost-and-usage --time-period Start=$(date -u +%Y-%m-01),End=$(date -u -v+1m +%Y-%m-01) --granularity MONTHLY --metrics UnblendedCost --profile "${profile}" | jq '.ResultsByTime[0].Total.UnblendedCost.Amount' | xargs printf "%'0.2f\n")
echo "Gathering cost forecast information for MONTH..."
month_cost_forecast=$(aws ce get-cost-forecast --time-period Start=$(date -u +%Y-%m-%d),End=$(date -u -v+1m +%Y-%m-01) --granularity MONTHLY --metric UNBLENDED_COST --profile "${profile}" | jq '.Total.Amount' | xargs printf "%'0.2f\n")
if [ -z $month_cost_forecast ]; then
month_cost_forecast="n/a"
fi
time_of_event=$(date -u)
printf "%s @ %s \n%s: $%s \n%s: $%s \n%s: $%s \nMONTH TO DATE: $%s \nFORECAST MONTH-END: $%s" "${profile}" "${time_of_event}" "${day_before_yesterday_date}" "${day_before_yesterday_cost}" "${yesterday_date}" "${yesterday_cost}" "${today_date}" "${today_cost}" "${month_to_date_cost}" "${month_cost_forecast}"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment