robfatland / costnotify

AWS billing accumulator lambda function against a cloudchekr S3 bucket called copydbr
MIT License
2 stars 2 forks source link

Status: This is a functional solution oriented towards a specific billing accumulator (DLT using Cloudcheckr). It can be improved in that context and it could be adapted to other billing accumulators.

Get started by clicking into the Instructions folder

costnotify

This repo is instructions and code for building an AWS lambda function costnotify to send a spending breakdown email to selected recipients for an AWS cloud account. Typically the email subject is "how much did I spend in the past 24 hours" and the email body is further details breaking down cost.

The lambda code is the file costnotify.py in this folder. Our instructions include a copy-paste of this code into the code window of the lambda function on the AWS console. There is another method for doing this using a tar file that we do not cover here.

Other files in this repo such as prototype.py and analysis.py are older version source material. We'll try and keep costnotify.py as our best working version.

Instructions for building out the costnotify lambda function are found in the instructions folder.

Additional considerations...

There are four AWS services attached to the costnotify Lambda function:

AWS Billing Notebook

A notebook to act as an interactive extension of costnotify and mirror the methods used on the Google Cloud Platform side of Pangeo. The notebook should be runnable for anyone on the us-east-1 Pangeo JupyterHub. It should also be usable for anyone who has configured the AWS CLI and is an admin on the project.

Running the notebook as-is will find all the billing files in the bucket and download all of them. There is a possibility that they could just be read into memory and worked with there, but downloading is easier for now.

There are a few convenient functions in the notebook for common visualizations: cost by day and cost by month. There is also a line of code to show a table of itemized costs over the time period you select.

There are two versions of the notebook. The base version of the notebook uses Boto3 to read in the data and Pandas / Matplotlib to visualize it. The newer version uses s3fs to read in the data and hvplot on top of Pandas / Matplotlib for visualization.

Prep

Within a candidate account: Check the S3 listing for N.Virginia to identify the logging bucket. In our case this bucket has a name that begins with copydbr-. The bucket should contain monthly billing itemization files.