Send CloudWatch alarms to Slack via AWS Lambda

06 / Jun / 2024 by Pranjal Tripathi 0 comments

Introduction

One of the key factors in maintaining production uptime is the implementation of effective monitoring and alert systems. Integrating AWS CloudWatch with Slack using AWS Lambda allows you to receive real-time alerts in your Slack channels, keeping your team informed and responsive. This blog will guide you do this integration using a AWS Lambda function for sending AWS CloudWatch alarms to Slack.

Objective

The article covers setting up an incoming webhook in your Slack workspace to receive messages from AWS and how to create an AWS Lambda function to process CloudWatch alarms and send formatted notifications to the Slack channel via the webhook URL.

Prerequisites

  • AWS Account
  • Slack

Steps by Step Procedure –

1. Creation of a SNS Topic

Cloudwatch uses SNS topics to publish messages. So, In your AWS console, visit Simple Notification Service, then enter the name of the topic that you want to create, and click on Next Step under the topic name.

Note: Select type as Standard and the topic name that you have entered earlier should be visible in the Name section, confirm that and create a topic.

2. Creation of Slack channel and Slack App

Now, Create a slack channel for receiving cloudwatch alerts. Let’s give it a name Test and choose visibility as per your need.


For creating a slack app, visit https://api.slack.com/apps and click on Create an app. Choose From scratch option for creating a slack app

Give a name to your app and select the same workspace in you have created the channel for receiving the alerts to develop your app.

We will be using webhooks for receiving alerts from cloudwatch. So, let’s activate incoming webhooks and the webhook to your channel in which you want to receive alerts.

Go to Basic Information and give basic details like the App icon and description to your app. It’s an optional step

3. Creation of Lambda Function

Go to the AWS Management Console and navigate to Lambda and click Create function, and choose Author from scratch. Provide basic details to your lambda function and select Python3.9 runtime as this the python version which our lambda function will be using.

After the creation of the Lambda, we need a trigger to invoke our lambda function whenever any Cloudwatch event takes place.

Select the topic that we have created earlier to send alerts to slack. AWS will add a resource-based policy to the function and add a subscription to the topic.

Now, Replace the below prewritten function with the below function and give your webhook url to slack_url variable, give your region to region variable and Deploy the function.



Note: You can find your webhook url in the Incoming Webhooks tab in your Slack application.

4. Create a Cloudwatch Alarm

Now, create a CloudWatch alarm and configure it to send notifications to the Lambda function via an SNS topic.

Go to the AWS CloudWatch console and navigate to the Alarms section. Click “Create Alarm” and choose the appropriate metric for your use case (e.g., CPU utilisation, memory usage, or custom metrics).

Set the desired threshold and configure the alarm’s behaviour (e.g., send a notification when the metric exceeds a certain value).

Under “Send a notification to,” select “SNS Topic” and choose the SNS Topic you created in the previous steps.

Review the alarm configuration and click “Create Alarm.”

For testing the CPU utilization, the Stress Linux utility can be used. Install it by running sudo apt install stress, and then for testing, execute stress –cpu 2.

Conclusion

You have now successfully set up an AWS Lambda function to send CloudWatch alarms to a Slack channel. This integration allows you to stay informed about your AWS resources’ health and performance in real-time, directly over your team slack channel.

Feel free to customize the Lambda function further as per your specific needs. For example, you can add more details to the Slack messages, handle different types of alerts differently, or integrate with other monitoring tools.

FOUND THIS USEFUL? SHARE IT

Tag -

aws lambda SLACK

Leave a Reply

Your email address will not be published. Required fields are marked *