Send slack notifications with AWS Chatbot

Davide de Paolis
4 min readJan 13, 2023

What I really love about AWS and Serverless is that over time new services pop up ( yeah well, this is not always a good thing, as pointed out by Corey Quinn) or features are added that make everything simpler and even cheaper.

For our project we always set up Metrics, Alarms and Notifications to be able to quickly react when something is behaving weirdly.
One of the way we set up our monitoring system was via Slack Notifications.

Old approach: SNS + Lambda

In order to send notifications to Slack, we used SNS and Lambda.

In our CDK stack we:

  • created an SNS Topic
  • created a Lambda function in charge of sending out the notification to Slack
  • created a subscription to the above topic for our lambda

In order to send messages to Slack, you need to enable WebHooks, see here.

Our Lambda function needed that webhook URL, together with the ID of the Channel we wanted to send notifications too ( and we decided to pass them via CDK context.json.

Our Lambda was not doing much actually, just grabbing the message from the SNS Event it receives as payload — as described here — and then using Axios (but any HTTP client would do) to send an HTTP request to slack webhook.

As you can see above, we had to manipulate a bit the SNS message object in order to extract meaningful information we wanted to display in the slack notification.

Then, in any project where we had metrics and alarm, we had to simply retrieve the Topic we created above and bind it to an Alarm Action.

New approach: SNS + AWS Chatbot

It was as you can see already simple enough, and we used that for years already. Recently I was setting up Slack Notifications for a new project on a new team and while I was checking out that old repo and some newer documentation I realised AWS ChatBot exists.

AWS Chatbot is an interactive agent that makes it easier to monitor and interact with your AWS resources in your Slack channels and chat channels. By using AWS Chatbot, you can receive alerts and run commands to return diagnostic information, invoke AWS Lambda functions, and create AWS Support cases so that your team can collaborate and respond to events faster.

Of course, Chatbot does a lot more than receiving Slack Notifications — actually, it is more a powerful tool to send messages from Slack in order to run some commands on your AWS resources (like launching deployments, starting instances, triggering lambdas and so on) but I will explain that in another post.

But for now, let’s focus on how Chatbot simplified our Monitoring system basically allowing us to get rid of the Lambda function.

In our new SlackNotification stack, we still create a Topic, but then instead of creating a Lambda, you set up the configuration of your SlackBot.

beware: it is not possible to create a new SlackChannelConfiguration on a Slack Channel already assigned to another configuration, so make sure you create it in a separate specific repository, and you just reference it in other project ( something that we also did for the old approach, anyway)

Once you have your Slackbot set up, wherever we want to send slack notifications when an Alarm enters Error state, we do as before.
In any project just get the topic from its ARN and attach an Action to your alarm.

But you are just exchanging Lambda with Chatbot, where’s the gain?

I might argue that you have less code to maintain, and easier infrastructure, but that wouldn’t be a great selling point — we are talking about one single Lambda, which we haven’t touched in years — so let me say just this:

There is no additional charge for AWS Chatbot. You pay only for the underlying services. source

So, basically, by getting rid of the Lambda, you are not going to pay for all the lambda invocations for every single slack notification, and you only pay, as before, only for the use of SNS.
Again, hopefully, you do not have millions of slack notifications every day, so costs could be quite irrelevant, but if you can have a simpler system for less money, why not?
Even migrating takes few minutes ( it definitely took me longer to write this post!)

where do I find Slack IDs?

the easiest way to find both the WorkspaceID and the ChannelID is to open your Slack Application in your browser and go to the channel you want to become your recipient. The URL will be something like this — https://app.slack.com/client/WORKSPACE_ID/CHANNEL_ID.

While reading Slack documentation I found another option you might find interesting. It seems you can send emails directly to slack by simply getting an email for your channel:

Since SNS can send emails without many hassles or configuration overhead it might be an easy solution. I haven’t tried that though, so I can tell you much more. (feel free to comment below if you have experience and opinions)

Other posts you might find interesting:

Foto by Stephen Phillips — Hostreviews.co.uk on Unsplash

Originally published at https://dev.to on January 13, 2023.

--

--

Davide de Paolis

Sport addicted, productivity obsessed, avid learner, travel enthusiast, expat, 2 kids. Technical Lead (NodeJs Serverless) | AWS Community Builder