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
- First I created a slack workspace for testing this
- Then I created a channel just for reports : #reports
- Then I added the app called Incoming Webhook
- 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
- Created a Lambda function (of course in Python :D) which would use boto’s cost explorer package to get the daily usage.
- 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.
- After that I wrote code to parse the response to get the “UnBlended” cost in USD for that day.
- 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”
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.
Boom ! 🙂 Have also created a cloudwatch alarm version of this.