Skip to main content

CloudWatch Alarms β†’ Slack Integration Reference

πŸ“Š Architecture Overview

πŸ”„ Step-by-Step Flow

Step 1: CloudWatch Alarm Triggers

Example Alarm Configuration:

Step 2: SNS Topic Receives & Publishes

SNS Message Format (JSON):
SNS has a subscription:
This tells SNS: β€œWhenever you receive a message, invoke this Lambda function”

Step 3: Lambda Function Processes

Lambda receives the SNS event and:
  1. Extracts the CloudWatch alarm details from SNS message
  2. Parses alarm information (name, state, reason, metrics)
  3. Formats into a beautiful Slack message with colors and emojis
  4. Sends HTTP POST to Slack webhook URL
Lambda Code Flow:

Step 4: Slack Receives Notification

Slack Message Format:

πŸ” Permissions & Security

Lambda IAM Role Permissions

SNS β†’ Lambda Permission

This permission says: β€œSNS topic can trigger this Lambda function”

πŸ“¦ Resources Created

πŸ§ͺ Testing Flow

Test 1: Slack Webhook (Direct)

βœ… Verifies: Slack webhook URL is valid

Test 2: Lambda Function (Direct)

βœ… Verifies: Lambda can parse messages and send to Slack

Test 3: SNS Topic β†’ Lambda

βœ… Verifies: SNS β†’ Lambda integration works

Test 4: CloudWatch Alarm β†’ SNS

βœ… Verifies: Complete end-to-end flow

🎨 Message Formatting

Color Coding

Emojis Used

Message Structure

πŸ”§ Configuration

Environment Variables

Customization Options

Change Slack Channel:
  • Generate new webhook URL from Slack
  • Update slack_webhook_url in locals block
Modify Message Format:
  • Edit lambda_function.py
  • Update the format_slack_message() function
Add More Details:
  • Parse additional fields from CloudWatch alarm JSON
  • Add to Slack message payload
Filter Alarms:
  • Add logic in Lambda to filter by alarm name prefix
  • Only send specific alarms to Slack

πŸ“Š Monitoring & Troubleshooting

CloudWatch Logs

Common Issues

❌ Slack message not appearing:
  • Check Lambda logs: /aws/lambda/CloudWatch-Alarms-To-Slack
  • Verify webhook URL is correct
  • Check Lambda execution errors
❌ Lambda not triggered:
  • Verify SNS subscription is β€œConfirmed” (not Pending)
  • Check Lambda permission for SNS
  • Verify alarm has SNS topic in alarm_actions
❌ Message format broken:
  • Check CloudWatch alarm JSON structure
  • Verify Lambda Python code syntax
  • Test with sample alarm JSON

πŸ’° Cost Estimation

Monthly Costs (Approximate)

πŸ“š References

πŸš€ Deployment Checklist

  • Slack webhook URL configured
  • Lambda function code created (lambda_function.py)
  • Terraform configuration created (SLACK-ALARMS.tf)
  • Variables configured (variables.tf)
  • Run terraform plan to preview changes
  • Run terraform apply to deploy
  • Test Lambda function manually
  • Verify SNS subscription is confirmed
  • Trigger a test alarm to verify end-to-end flow
  • Monitor CloudWatch Logs for any errors