Serverless Slack Bot for AWS Billing Alerts

Rafael Nunes
Apr 28 · 4 min read

Almost every company is using slack internally. People want to use slack bots to handle their daily tasks including alerting or triggering some kind of events.

In this blogpost, we will create a slack bot using only serverless technologies. Users can define alerts and get notified when alerts are triggered by using this bot. In order to keep alert rules we need a state store. So here we go, we will use Serverless Redis, from Lambda Store

Image for post
Image for post

Prerequisites

Our architecture diagram will be as following:

Creating Initial Project and Slack Bot

You can create an example project using following command:

serverless create — template aws-go-mod

In slack admin panel, we will create a bot and will add Event Subscriptions (in order to listen commands to bot) and Incoming Webhooks (in order to send messages to slack channel). For event subscriptions, we will put our lambda api gateway url when we deploy the application ( So slack can send commands to our lambda function). For now, let’s create initial bot and configure some basics details.

Image for post
Image for post

Create Serverless Redis Database

From Lambda Store you can create a free/paid serverless database in less than 15 seconds. After database is created, you will get configuration in order to make connection.

Image for post
Image for post

In our lambda functions, inside Init method we need to initialise our redis connection:

Handling Commands From User

In order to listen commands from the user, we will use event subscription feature of our bot. Slack automatically sends all commands to our lambda function when we send a message to our bot.

Let’s create an AcceptCommand function and make it as post request. In order to verify coming request, Slack will send challenge token, we need to respond the slack with coming token.

After we got request, we need to create a slack event object from the request body. We can use official slack golang sdk.

slackevents.ParseEvent functions parses the body and returns the event. Then we are going to cast the event data to slackevents.AppMentionEvent because we are sending commands to our but by mentioning.

Now we have the event and we can look the command of the user. In order to make it simple, I will only accept 2 commands, ADD and LIST. We can just scan the event.Text and parse the service name and threshold.

Image for post
Image for post
ADD command
Image for post
Image for post
LIST command

Periodically Check Alerts and Notify User

We will also create another function for periodically checking alerts and thresholds. I have created a Check function that checks all keys in the Redis and checks each alert rule.

First we are doing a API call to AWS and get all cost metrics. After that we are looking each cost and check whether there is an alert defined or not.

Lets first make an API call to AWS

Image for post
Image for post

After we got billing API response then we can check each alert from the our state store which is serverless redis:

Conclusion

Now, it is very easy to write a lambda function and connect it to a slack command or bot. But lambda functions are stateless so we cannot keep list of the alerts inside the lambda function. We need a storage system. It can be either S3 or Dynamodb or RDS or redis. In this blog, we have used serverless redis in order to show how it is easy to use and play with it.

You can deploy the bot using following command ( please fill necessary fields in serverless.yml file :

make deploy

In this blog, I have used couple of libraries, you can see list of all libraries from go.mod file.

You can find source code from here: https://github.com/Lambda-Store/aws-billing-slack-bot

Lambda Store

Everything on serverless and cloud

Rafael Nunes

Written by

software architect @ https://lambda.store https://twitter.com/devnunes

Lambda Store

Lambda Store is the first Serverless Database for Redis. In this blog, Lambda Store engineering team shares their experiences on Cloud, AWS, Kubernetes, Redis and of course Lambda Store. https://lambda.store

Rafael Nunes

Written by

software architect @ https://lambda.store https://twitter.com/devnunes

Lambda Store

Lambda Store is the first Serverless Database for Redis. In this blog, Lambda Store engineering team shares their experiences on Cloud, AWS, Kubernetes, Redis and of course Lambda Store. https://lambda.store

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store