Skip to main content

title: “CloudWatch to Slack Alerting” description: “Setup CloudWatch alarms for EC2 and ALB with enriched notifications sent to Slack via SNS and Lambda”

📘 CloudWatch → Slack Alerting Guide

Scope: Setup CloudWatch alarms for EC2 CPU utilization and ALB 4xx/5xx errors, centralize alarms via SNS + Lambda, and send enriched notifications to Slack with alarm name, resource details, and deep links.

1. Prerequisites

  • AWS Account with IAM admin or equivalent privileges.
  • Slack workspace with permission to create Apps or use Incoming Webhooks.
  • Python-based AWS Lambda runtime (3.9+ recommended).
api.slack.com hooks.slack.com

Slack API Documentation for Webhooks APP

https://docs.slack.dev/quickstart

2. Architecture Overview

  • CloudWatch Alarms: Detect threshold breaches (CPU, ALB 4xx/5xx).
  • SNS Topic: Aggregates all alarms → single event pipeline.
  • Lambda Function: Parses alarm event, enriches with details, and posts to Slack.
  • Slack: Receives structured, actionable alerts.

3. Step 1 — Create an SNS Topic

  • Note the Topic ARN (e.g., arn:aws:sns:ap-south-1:123456789012:cloudwatch-slack-topic).

4. Step 2 — Create CloudWatch Alarms

4.1 CPU Alarm (EC2)

  • Metric: AWS/EC2CPUUtilization.
  • Dimension: InstanceId=i-xxxxxxxx.
  • Threshold: > 80% for 5 minutes.

4.2 ALB 5xx Alarm

  • Metric: AWS/ApplicationELBHTTPCode_ELB_5XX_Count.
  • Dimension: LoadBalancer=app/my-alb/1234567890abcdef.
  • Threshold: > 10 errors in 5 minutes.

4.3 ALB 4xx Alarm

  • Same as 5xx, but HTTPCode_ELB_4XX_Count.

5. Step 3 — Setup Slack Webhook

Option A (Quick & Dirty)

  • Create an Incoming Webhook in Slack → get URL like: https://hooks.slack.com/services/TXXX/BXXX/XXXXXXXX
  • Create a Slack App → Enable Incoming Webhooks → Install to Workspace → Copy Webhook URL.

6. Step 4 — Create Lambda Function

6.1 Lambda IAM Role

Grant Lambda access to:
  • AWSLambdaBasicExecutionRole.
  • AmazonSNSFullAccess (or SNS Subscribe).

6.2 Lambda Code (Python 3.9+)


7. Step 5 — Connect SNS → Lambda

Then give Lambda permission to be invoked by SNS:

8. Step 6 — Testing

  • Manually set thresholds very low (e.g., CPU > 1%) to trigger alarms.
  • Verify messages in Slack include:
    • Alarm Name
    • State (ALARM / OK)
    • Metric Name
    • Resource ID
    • AWS Region + Account
    • Direct CloudWatch Console Link
    • Reason

9. Enhancements (Optional)

  • Extend Lambda to include top failing ALB paths using ALB Access Logs + CloudWatch Logs Insights.
  • Route different alarms to different Slack channels by mapping AlarmNameSlack Webhook.
  • Add severity levels (critical, warning, info) based on alarm type.

✅ Executive Summary

  • CPU alarms tell you which EC2 instance is overutilized.
  • ALB 4xx/5xx alarms tell you which load balancer is serving bad requests.
  • Slack notifications are enriched with alarm name, metric, resource ID, and direct AWS links.
  • A single Lambda function manages all alarms, making operations lean and scalable.