Giter Club home page Giter Club logo

aws-cost-explorer-report's Introduction

AWS Cost Explorer Report Generator

Python SAM Lambda module for generating an Excel cost report with graphs, including month on month cost changes. Uses the AWS Cost Explorer API for data.

screenshot

License Summary

This sample code is made available under a modified MIT license. See the LICENSE file.

AWS Costs

Prerequisites

Docker (optional for building the lambda python package with updated https://pypi.python.org/ third-party libraries)

Easy Deploy (us-east-1 only)

If you do not need to modify the code, just deploy the included easy_deploy.yaml using AWS Cloudformation via the console.

Deploying (SAM / Script)

Update the values in deploy.sh for your AWS account details.

Variable Description
S3_BUCKET S3 Bucket to use
SES_SEND Email list to send to (comma separated)
SES_FROM SES Verified Sender Email
SES_REGION SES Region
COST_TAGS List Of Cost Tag Keys (comma separated)
CURRENT_MONTH true / false for if report does current partial month
DAY_MONTH When to schedule a run. 6, for the 6th by default
TAG_KEY Provide tag key e.g. Name
TAG_VALUE_FILTER Provide tag value to filter e.g. Prod*
LAST_MONTH_ONLY Specify true if you wish to generate for only last month

And then run sh deploy.sh

Deploy Manually (Lambda Console)

  1. Create a lambda function (python 3.8 runtime), and update the code to the contents of src/lambda.py
  2. Create a lambda IAM execution role with ce:, ses:, s3:, organizations:ListAccounts
  3. Configure the dependency layer: arn:aws:lambda:us-east-1:749981256976:layer:CostExplorerReportLayer:1
  4. Update ENV Variables in Lambda console
    • Details in table above.
  5. Create a trigger (CloudWatch Event)

Running / Testing

Once the Lambda is created, find it in the AWS Lambda console. You can create ANY test event (as the event content is ignored), and hit the test button for a manual run.

https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-test-function.html

Building the Lambda Layer (Optional, for if you want to build your own AWS Lambda layer for use with the script)

Run build.sh to build a new AWS lambda layer with the required Python libraries. This requires Docker, as it builds the package in an Amazon Linux container.

sh build.sh

Customise the report

Edit the main_handler segment of src/lambda.py

def main_handler(event=None, context=None): 
    costexplorer = CostExplorer(CurrentMonth=False)
    #Default addReport has filter to remove Support / Credits / Refunds / UpfrontRI
    #Overall Billing Reports
    costexplorer.addReport(Name="Total", GroupBy=[],Style='Total',IncSupport=True)
    costexplorer.addReport(Name="TotalChange", GroupBy=[],Style='Change')
    costexplorer.addReport(Name="TotalInclCredits", GroupBy=[],Style='Total',NoCredits=False,IncSupport=True)
    costexplorer.addReport(Name="TotalInclCreditsChange", GroupBy=[],Style='Change',NoCredits=False)
    costexplorer.addReport(Name="Credits", GroupBy=[],Style='Total',CreditsOnly=True)
    costexplorer.addReport(Name="Refunds", GroupBy=[],Style='Total',RefundOnly=True)
    costexplorer.addReport(Name="RIUpfront", GroupBy=[],Style='Total',UpfrontOnly=True)
    #GroupBy Reports
    costexplorer.addReport(Name="Services", GroupBy=[{"Type": "DIMENSION","Key": "SERVICE"}],Style='Total',IncSupport=True)
    costexplorer.addReport(Name="ServicesChange", GroupBy=[{"Type": "DIMENSION","Key": "SERVICE"}],Style='Change')
    costexplorer.addReport(Name="Accounts", GroupBy=[{"Type": "DIMENSION","Key": "LINKED_ACCOUNT"}],Style='Total')
    costexplorer.addReport(Name="AccountsChange", GroupBy=[{"Type": "DIMENSION","Key": "LINKED_ACCOUNT"}],Style='Change')
    costexplorer.addReport(Name="Regions", GroupBy=[{"Type": "DIMENSION","Key": "REGION"}],Style='Total')
    costexplorer.addReport(Name="RegionsChange", GroupBy=[{"Type": "DIMENSION","Key": "REGION"}],Style='Change')
    if os.environ.get('COST_TAGS'): #Support for multiple/different Cost Allocation tags
        for tagkey in os.environ.get('COST_TAGS').split(','):
            tabname = tagkey.replace(":",".") #Remove special chars from Excel tabname
            costexplorer.addReport(Name="{}".format(tabname)[:31], GroupBy=[{"Type": "TAG","Key": tagkey}],Style='Total')
            costexplorer.addReport(Name="Change-{}".format(tabname)[:31], GroupBy=[{"Type": "TAG","Key": tagkey}],Style='Change')
    #RI Reports
    costexplorer.addRiReport(Name="RICoverage")
    costexplorer.addRiReport(Name="RIUtilization")
    costexplorer.addRiReport(Name="RIUtilizationSavings", Savings=True)
    costexplorer.addRiReport(Name="RIRecommendation")
    costexplorer.generateExcel()
    return "Report Generated"

aws-cost-explorer-report's People

Contributors

ashishgore avatar davfaulk avatar grusy avatar jpeddicord avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.