Amazon SNS
SNS publish/subscribe with topics and subscribers, the services that publish to it, how to publish, and its security.
What it is
What if the need is one message to many receivers? With direct integration, the buying service would call the email service, the fraud service, the shipping service and a queue, one by one, and need changing every time a receiver is added.
Amazon SNS (Simple Notification Service) uses publish/subscribe (pub/sub) instead:
- The event producer sends a message to one SNS topic.
- Event receivers (subscriptions) listen to the topic: as many as needed.
- Each subscriber gets every message (unless it uses a filter policy).
- Up to 12,500,000 subscriptions per topic, and 100,000 topics per account.
Subscribers
- SQS queues, Lambda functions, Amazon Data Firehose streams (to S3, Redshift…);
- email, SMS and mobile push notifications;
- HTTP(S) endpoints.
SNS pushes messages to subscribers; it doesn't store them. If a subscriber can't receive a message after SNS's retries, the message is lost for that subscriber (unless the subscription has a dead-letter queue). Put an SQS queue behind SNS when messages must be kept.
Services that publish to SNS
Many AWS services send notifications straight to an SNS topic:
- CloudWatch alarms, AWS Budgets, Lambda;
- Auto Scaling group notifications, S3 bucket events, DynamoDB;
- CloudFormation stack state changes, AWS DMS (new replication), RDS events, and more.
How to publish
- Topic publish (using the SDK):
- create a topic;
- create one or more subscriptions;
- publish to the topic.
- Direct publish (for mobile app SDKs):
- create a platform application;
- create a platform endpoint;
- publish to the platform endpoint. Works with Apple APNs, Google FCM, Amazon ADM and others.
Security
- Encryption:
- in flight, using the HTTPS API;
- at rest, with KMS keys;
- client-side, if the client wants to encrypt and decrypt itself.
- Access control: IAM policies control who can use the SNS API.
- SNS access policies (resource policies, like S3 bucket policies) are useful for:
- cross-account access to a topic;
- letting other services, such as S3, publish to a topic.
Real-world examples
- A CloudWatch alarm on high CPU publishes to an SNS topic that emails the on-call engineer and sends an SMS.
- An online shop publishes each order to a topic; the warehouse, invoicing and analytics systems each subscribe.
- A mobile game uses SNS mobile push to tell players their turn has come.
Exam tips
- "Notify many receivers of one event", "pub/sub" → SNS topic.
- "Email or SMS when a CloudWatch alarm fires" → CloudWatch alarm → SNS.
- "Mobile push notifications" → SNS.
- "Allow S3 (or another account) to publish to the topic" → SNS access policy.
Common traps
- "SNS stores messages for subscribers that are offline." It doesn't; use SQS behind SNS.
- "Consumers poll SNS." SNS pushes to subscribers.
Hands-on lab: an email notification
Goal: create a topic, subscribe your email address and publish a message.
Costs money
The first 1,000 email notifications each month are free, and a few API calls cost nothing measurable. Any Region works.
Create a topic
SNS → Topics → Create topic, type Standard, name saa-alerts. Create.
Subscribe your email
Create subscription: protocol Email, endpoint your email address. Open the confirmation email and choose Confirm subscription.
Publish
Back on the topic, Publish message: subject Test, body Hello from SNS. Publish. The email arrives within seconds.
Expected result: one publish delivered to your inbox through the topic.
Clean up: keep saa-alerts for the fan-out lab, or delete the subscription and the topic.
AWS documentation
SQS FIFO and patterns
SQS FIFO queues with ordering and deduplication, dead-letter queues, delay queues, and SQS as a buffer in front of a database.
SNS fan-out and filtering
The SNS plus SQS fan-out pattern, sending S3 events to several queues, SNS to S3 through Firehose, SNS FIFO topics, and message filtering.