Skip to main content

Quick Reference: How SNS Works with Lambda for Slack Notifications

🎯 The Simple Version

What Happens When an Alarm Triggers?

πŸ”‘ Key Concepts

SNS (Simple Notification Service)

  • Think of it as: A message router/broadcaster
  • What it does: When it receives a message, it forwards it to all subscribers
  • Subscribers can be: Lambda, Email, SMS, HTTP endpoints, etc.
  • In our case: CloudWatch β†’ SNS β†’ Lambda

Why use SNS instead of CloudWatch β†’ Lambda directly?

βœ… Flexibility: You can add multiple subscribers (email, Lambda, etc.) βœ… Decoupling: CloudWatch doesn’t need to know about Lambda βœ… Fan-out: One alarm can notify multiple destinations βœ… Retry logic: SNS handles retries if Lambda fails

Lambda Subscription to SNS

This creates a subscription that says:
β€œHey SNS topic! Whenever you receive a message, please invoke this Lambda function”

Permission for SNS to Call Lambda

This permission says:
β€œLambda function, please allow SNS to invoke you”

πŸ“Š Data Flow Example

CloudWatch Alarm Data

SNS Wraps it

Lambda Receives & Processes

Slack Receives

πŸŽͺ All Components Working Together

πŸ”„ Why This Architecture?

Alternative 1: CloudWatch β†’ Lambda directly

❌ Not supported by AWS (CloudWatch can’t directly invoke Lambda for alarms) ❌ Would need custom EventBridge rules (more complex)

Alternative 2: CloudWatch β†’ Email

❌ Not pretty, just plain text ❌ No formatting or colors ❌ Hard to filter/organize

Our Solution: CloudWatch β†’ SNS β†’ Lambda β†’ Slack

βœ… Standard AWS pattern (SNS is designed for this) βœ… Flexible (can add email, SMS, etc. to SNS later) βœ… Pretty Slack messages with formatting βœ… Easy to customize Lambda code βœ… Reliable (SNS handles retries)

πŸ“ Summary

SNS is the glue that connects CloudWatch alarms to Lambda functions. Think of SNS as a notification hub:
  • CloudWatch says: β€œHey SNS, I have an alarm!”
  • SNS says: β€œThanks! Let me notify all my subscribers”
  • Lambda (as subscriber) says: β€œGot it! Sending to Slack…”
Without SNS, you’d need complex EventBridge rules and more configuration. With SNS, it’s a simple, standard AWS pattern that works reliably.

πŸš€ Ready to Deploy?

Run these commands:
Your alarms will automatically flow through this pipeline: CloudWatch β†’ SNS β†’ Lambda β†’ Slack πŸŽ‰