CloudWatch Alarms β Slack Integration Reference
π Architecture Overview
π Step-by-Step Flow
Step 1: CloudWatch Alarm Triggers
Step 2: SNS Topic Receives & Publishes
Step 3: Lambda Function Processes
- Extracts the CloudWatch alarm details from SNS message
- Parses alarm information (name, state, reason, metrics)
- Formats into a beautiful Slack message with colors and emojis
- Sends HTTP POST to Slack webhook URL
Step 4: Slack Receives Notification
Slack Message Format:π Permissions & Security
Lambda IAM Role Permissions
SNS β Lambda Permission
π¦ Resources Created
| Resource | Name | Purpose |
|---|---|---|
| IAM Role | CloudWatch-Alarms-To-Slack-Role | Lambda execution role |
| Lambda Function | CloudWatch-Alarms-To-Slack | Processes alarms & sends to Slack |
| CloudWatch Log Group | /aws/lambda/CloudWatch-Alarms-To-Slack | Lambda execution logs |
| SNS Subscription | (auto-generated) | Connects SNS β Lambda |
| Lambda Permission | AllowExecutionFromSNS | Allows SNS to invoke Lambda |
π§ͺ Testing Flow
Test 1: Slack Webhook (Direct)
Test 2: Lambda Function (Direct)
Test 3: SNS Topic β Lambda
Test 4: CloudWatch Alarm β SNS
π¨ 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_urlinlocalsblock
- Edit
lambda_function.py - Update the
format_slack_message()function
- Parse additional fields from CloudWatch alarm JSON
- Add to Slack message payload
- 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
- Verify SNS subscription is βConfirmedβ (not Pending)
- Check Lambda permission for SNS
- Verify alarm has SNS topic in
alarm_actions
- 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 planto preview changes - Run
terraform applyto 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
