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
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.
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.
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.
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
After we got billing API response then we can check each alert from the our state store which is serverless redis:
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 :
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