AWS Spend Report
What will I learn? | In this Tutorial we will create a simple report that shows our recent spend on AWS for the last six months, broken down by AWS service. |
Difficulty |
What you'll need
Software & Services
- Docker version 16.14 or above
- An AWS User or Role with permissions to use the AWS CostExplorer API
- An active AWSH from AWSH Intro with some AWS credentials loaded
Permissions & Access
Below is a basic IAM Policy based on the one from the AWS documentation here that covers the permissions needed for this tutorial
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ce:*"
],
"Resource": [
"*"
]
}
]
}
Tutorial
In each of the examples below we will take advantage of several of the environment variables that are loaded and set when we activate a set of credentials in AWSH
env | grep ^AWS_
AWS_ACCOUNT_ALIAS=dummy-account
AWS_DEFAULT_REGION=eu-central-1
AWS_SESSION_EXPIRATION=2022-11-28 16:05:01
AWS_SECRET_ACCESS_KEY=f3hfuss77fffdUToVx+T1iFNV+6tLgL3dUhCJY6jhPccRQW
AWS_ID_NAME=dummy-account/[email protected]
AWS_ACCOUNT_NUMBER=011111221051
AWS_ACCESS_KEY_ID=ASIAQO7RUGABCDEFYXLOK
AWS_TOKEN_EXPIRY=1669651501
We will use these environment variables to generate useful report names
This tutorial also makes use of one of the built-in commands available in AWSH awsh list costs-by-month-and-service
which allows us to generate our output report in multiple formats. To find out more about all of the helper commands and utilities available in AWSH check out the full documentation here.
Markdown Format Report
Create a Markdown header for our report
AWSH❯cat > "${HOME}/workspace/${AWS_ACCOUNT_NUMBER}-AWS-Spend.md" <<EOF
# AWS Spend Report for A/C: ${AWS_ACCOUNT_NUMBER}
> This spend report was generated using AWSH on $(date) for
> AWS Account ${AWS_ACCOUNT_NUMBER} with alias: ${AWS_ACCOUNT_ALIAS}
EOFGet the data from the AWS API using AWSH
AWSH❯awsh list costs-by-month-and-service -f pipe >> "${HOME}/workspace/${AWS_ACCOUNT_NUMBER}-AWS-Spend.md"
The Markdown format report is plain text but can be converted into many other common formats (HTML, PDF, MS Word, etc). One of our favourite online converters is https://dillinger.io/ or the extensions available for Microsoft VS Code

CSV Format Report
Get the data from the AWS API using AWSH and output as CSV
AWSH❯awsh list costs-by-month-and-service -f csv > "${HOME}/workspace/${AWS_ACCOUNT_NUMBER}-AWS-Spend.csv"
Screenshot: CSV Report
HTML Format Report
Create an HTML header for our report
AWSH❯cat > "${HOME}/workspace/${AWS_ACCOUNT_NUMBER}-AWS-Spend.html" <<EOF
<html>
<head>
<title>AWS Spend Report for A/C: ${AWS_ACCOUNT_NUMBER}</title>
</head>
<body>
<h1>AWS Spend Report for A/C: ${AWS_ACCOUNT_NUMBER}</h1>
<p>
This spend report was generated using AWSH on $(date)
for AWS Account ${AWS_ACCOUNT_NUMBER} with alias: ${AWS_ACCOUNT_ALIAS}
</p>
<div>
EOFGet the data from the AWS API using AWSH
AWSH❯awsh list costs-by-month-and-service -f html >> "${HOME}/workspace/${AWS_ACCOUNT_NUMBER}-AWS-Spend.html"
Add the HTML page footer to make a valid HTML page
AWSH❯echo "</div></body></html>" >> "${HOME}/workspace/${AWS_ACCOUNT_NUMBER}-AWS-Spend.html"
Screenshot: HTML Report
Now that you've had a look around why not check out the full documentation here.
Did you know that both AWSH and BLOX are completely Open Source? That's right - the software is available free of charge, and we make some of our revenue by helping others install, use, and troubleshoot it. Take copy for yourself!
AWSH: Containerized tools with lightweight access to AWS CLI and APIs in mind.
BLOX: Containerized tools for engineering teams who need to work with AWS and IAC every day.