Daily AWS usage notifier on Slack

https://github.com/darshan-raul/Slack-Notifier-AWS-Lambda.git

It was painful to go to the AWS billing dashboard everyday to go and see the usage and cost of services I used yesterday. Too anxious you say? Well say that to someone who was hacked and got a 80k bill in an hour 🙂

So yup, I got a bad habit of checking the billing dashboard everyday. And you know what has to be done with tasks we do everyday ??

Automate it 😀

So I planned to create a Slack bot which would send me my daily usage everyday. Here’s how I did it.

#On Slack End

  1. First I created a slack workspace for testing this
  2. Then I created a channel just for reports : #reports
  3. Then I added the app called  Incoming Webhook
  4. After integrating it with the app I got a webhook url . This webhook url is all I needed to invoke to send data to the slack channel

# On AWS end

  1. Created a Lambda function (of course in Python :D) which would use boto’s cost explorer package to get the daily usage.
  2. It needed the start time and end time parameters which I needed to automate , So I used the datetime python package to get yesterday’s and today’s date.
  3. After that I wrote code to parse the response to get the “UnBlended” cost in USD for that day.
  4. This data had to be sent to Slack. I created a normal post request to the webhook url using python requests library . That was all that was needed from Lambda end. Now it was time to make sure that this code was triggered everyday

Enter “Cloudwatch Events

2019-04-25 10_45_28-Window.png

I created a Schedule as a event source while creating the cloudwatch event with the following cron 0 0 * * ? *.

Which meant that the event will be triggered everyday at 00:00 GMT. https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html

Once thats done I attached this to the Lambda function and tested it.

Slack bill

Boom ! 🙂 Have also created a cloudwatch alarm version of this.

Leave a comment

Design a site like this with WordPress.com
Get started