Skip to content

Instantly share code, notes, and snippets.

@MasWag
Created April 28, 2026 10:26
Show Gist options
  • Select an option

  • Save MasWag/493121a3041ed1ae61a37d32be366629 to your computer and use it in GitHub Desktop.

Select an option

Save MasWag/493121a3041ed1ae61a37d32be366629 to your computer and use it in GitHub Desktop.
Fetches and displays the monthly total AWS billing using the AWS Cost Explorer API.
#!/bin/sh
######################################
# NAME
# aws_monthly_billing - Fetches and displays the monthly total AWS billing using the AWS Cost Explorer API.
#
# SYNOPSIS
# aws_monthly_billing [AWS_CLI_OPTIONS]
#
# DESCRIPTION
# This script retrieves the monthly total AWS billing by querying the AWS Cost Explorer API. The script defaults to the current month but allows users to specify a custom time period using environment variables START and END.
#
# AUTHOR
# Masaki Waga
#
# LICENSE
# Apache License, Version 2.0
######################################
set -eu
# Set default time period to the current month unless specified by the user
START=${START:-$(date +%Y-%m-01)}
if [ "$(uname)" = Darwin ]; then
# macOS
END=${END:-$(date -v+1d +%Y-%m-%d)}
else
# Linux
END=${END:-$(date -d tomorrow +%Y-%m-%d)}
fi
aws ce get-cost-and-usage "$@" \
--time-period Start="$START",End="$END" \
--granularity MONTHLY \
--metrics UnblendedCost |
jq -r '.ResultsByTime[] | "Total usage from " + .TimePeriod.Start + " to " + .TimePeriod.End + ": " + .Total.UnblendedCost.Amount + " " + .Total.UnblendedCost.Unit'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment