Send CloudWatch Alarms to Slack with AWS Lambda
As we all know, things go wrong. That’s why monitoring and alerting are essential topics. Wouldn’t it be nice, if problems in your AWS account would show up in Slack? So you can react quickly while using your favorite messaging tool. In this blog post, you will learn how you can turn CloudWatch Alarms into Slack messages like this:
How it works
On AWS, everything sends monitoring data (CPU utilization, estimated monthly charges, …) to CloudWatch. In CloudWatch, you define alarms to send a message to an SNS topic if the monitoring data gets out of normal bounds. Finally, you connect a Lambda function to the SNS topic to trigger a function execution. The Lambda function calls the Slack API to send a message. The following figure shows the data flow:
To deploy the components in the figure, you will use the Serverless Application Model (SAM). If you are not interested in implementing this on your own, give our Slack chatbot a try. Never miss an alert from your AWS infrastructure with marbot!
Implementing the Lambda function
You will use Node.js to implement the Lambda function. To send a request to the Slack API, you have to make an HTTPS request. The request
module is easy to use, but I wanted a variant of the module that returns promises to avoid callback hell. That’s why I used request-promise-native
. The Slack webhook URL is passed in as an environment variable that you define later in the CloudFormation template.
const request = require('request-promise-native'); |
Messages delivered from SNS to the Lambda function will look like this:
{ |
You need to convert the format into the Slack message format.
const processRecord = (record) => { |
Finally, each Lambda function needs a handler function. The handler function takes 3 parameters:
- event
- comntext
- callback function
exports.event = (event, context, cb) => { |
Now, you will use SAM and CloudFormation to define the AWS resources.
Defining the CloudFormation template using SAM
First, you need some boilerplate to enable SAM and configure CloudFormation. Additionally, you will add two parameters to the template:
|
You need three resources in your template that:
- SNS Topic
- Lambda Function
- CloudWatch Alarm
SAM handles to SNS subscription and also takes care of IAM roles and Lambda permissions.
Resources: |
That’s it. You can find the full source code on GitHub. Now you can deploy the solution.
Deploying the solution
Slack setup
- Start by setting up an incoming webhook integration in your Slack workspace: https://my.slack.com/services/new/incoming-webhook/
- Select a channel or create a new one
- Click on Add Incoming WebHooks integration
- You are redirected to a new page where you can see your Webhook URL. Copy the value; you will need it soon.
AWS setup
- Clone or download the sample repository
- Create a S3 bucket for SAM (replace
$UniqueSuffix
with e.g. your username):aws --region us-east-1 s3 mb s3://cw-to-slack-$UniqueSuffix
- Install Node.js dependencies:
npm install
- Package the Lambda function code (replace
$UniqueSuffix
with e.g. your username):aws --region us-east-1 cloudformation package --s3-bucket cw-to-slack-$UniqueSuffix --template-file template.yml --output-template-file template.sam.yml
- Deploy the CloudFormation stack (replace
$WebhookURL
with your URL from Slack):aws --region us-east-1 cloudformation deploy --parameter-overrides "WebhookURL=$WebhookURL" --template-file template.sam.yml --stack-name cw-to-slack --capabilities CAPABILITY_IAM
Further steps
Many features are missing like closing incoming alerts, escalation schedules, and grouping of similar alerts. You can start to implement them on your own, or you can use marbot to send CloudWatch alarms to Slack.
marbot is optimized for small teams. Andreas and I build marbot during the Serverless Chatbot Competition 2016. We even won a prize!
Take care of your CloudWatch Alarms with marbot. Start today for free!
Further reading
- Article Passwordless database authentication for AWS Lambda
- Article Engaging your users with AWS Step Functions
- Article Lessons learned: Serverless Chatbot architecture for marbot
- Article AWS Security Primer
- Tag lambda
- Tag serverless
- Tag cloudwatch