Sending CloudWatch Alarms to Slack via SNS and AWS Lambda using Terraform

Listen to this Post

Monitoring your AWS account is crucial for maintaining the stability of your applications. One effective way to achieve this is by using Amazon CloudWatch Alarms to send notifications to Slack when issues arise. This article demonstrates how to link a Simple Notification Service (SNS) topic with a CloudWatch Alarm, which then triggers an AWS Lambda function to send a message to Slack with the alarm details.

You Should Know:

1. Create a CloudWatch Alarm:

aws cloudwatch put-metric-alarm \
--alarm-name "CPU-Alarm" \
--metric-name "CPUUtilization" \
--namespace "AWS/EC2" \
--statistic "Average" \
--period 300 \
--threshold 80 \
--comparison-operator "GreaterThanOrEqualToThreshold" \
--dimensions "Name=InstanceId,Value=i-1234567890abcdef0" \
--evaluation-periods 2 \
--alarm-actions "arn:aws:sns:us-east-1:123456789012:MyTopic"

2. Create an SNS Topic:

aws sns create-topic --name MyTopic

3. Subscribe to the SNS Topic:

aws sns subscribe \
--topic-arn "arn:aws:sns:us-east-1:123456789012:MyTopic" \
--protocol "email" \
--notification-endpoint "[email protected]"

4. Create an AWS Lambda Function:

aws lambda create-function \
--function-name "SlackNotifier" \
--runtime "python3.8" \
--role "arn:aws:iam::123456789012:role/lambda-execution-role" \
--handler "lambda_function.lambda_handler" \
--zip-file "fileb://lambda_function.zip"

5. Deploy Infrastructure using Terraform:

provider "aws" {
region = "us-east-1"
}

resource "aws_sns_topic" "alarm_notifications" {
name = "MyTopic"
}

resource "aws_lambda_function" "slack_notifier" {
function_name = "SlackNotifier"
handler = "lambda_function.lambda_handler"
runtime = "python3.8"
role = aws_iam_role.lambda_exec.arn
filename = "lambda_function.zip"
}

resource "aws_cloudwatch_metric_alarm" "example" {
alarm_name = "CPU-Alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 300
statistic = "Average"
threshold = 80
alarm_actions = [aws_sns_topic.alarm_notifications.arn]
dimensions = {
InstanceId = "i-1234567890abcdef0"
}
}

What Undercode Say:

Monitoring AWS resources effectively is essential for maintaining application stability. By integrating CloudWatch Alarms with SNS and AWS Lambda, you can automate notifications to Slack, ensuring that your team is promptly informed of any issues. This setup not only enhances your monitoring capabilities but also leverages Terraform for infrastructure as code, making it reproducible and scalable. Additionally, understanding and utilizing AWS CLI commands for creating alarms, SNS topics, and Lambda functions can significantly streamline your cloud operations. For further reading, you can refer to the AWS CloudWatch Documentation and Terraform AWS Provider Documentation.

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

Whatsapp
TelegramFeatured Image