Skip to main content

AWS Spend Report

In a Nutshell
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

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

AWSHTutorialPolicy
{
"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

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}

    EOF

  • Get 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

Markdown Report
Screenshot: Markdown Report

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"
    CSV Report
    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>
    EOF

  • Get 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"
    HTML Report
    Screenshot: HTML Report

Now that you've had a look around why not check out the full documentation here.


Want to find out more?

At Hestio, we have taken our experience with designing and building on cloud to codify these patterns and made them available as a low-code pattern library for AWS. Why spend time and effort on reinventing the wheel when it's already a solved problem? Would you start developing office productivity software in a world where Microsoft Office already exists?

If you'd like to find out about worX, our low-code patterns library for AWS you can read more here or get in touch today to schedule a demo.

If you'd like to find out more about the products and services Hestio has to offer, select one of the options below.