Amazon Simple Notification Service (SNS) is a communication tool used to send alerts when specified events happen. It fills a small but important niche within the cloud application space, and that niche is event alerts.
SNS is geared towards internal notifications, rather than communication to end users. For end user communication, Amazon Pinpoint can be used, as it offers a similar but more expansive set of functionality with far more customer centric features, like customizable emails.
In general, AWS services are very modular, and no one service can work without relying on at least one other service. This is very true in the case of SNS, as it is a notification tool, but generally notifications are not created directly within it. Its job is simply to track lists of recipients, their chosen methods of communication and to deliver the messages.
A note on cost: SNS offers different pricing for each supported communication channel. As with most AWS services, there is a free tier. In this case the first 5,000 monthly target audience communications on each channel is free. SNS pricing model.
The essential concepts in SNS are Publishers, Topics and Subscribers.
aws sns create-topic --namehttps://docs.aws.amazon.com/cli/latest/reference/sns/create-topic.html
Once you have at least one topic, you can start adding subscribers. Being subscribed to a topic allows the subscriber to receive any message a publisher sends to that topic.
aws sns subscribe \ --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic \ --protocol email \ --notification-endpoint firstname.lastname@example.org \ --region us-west-2https://docs.aws.amazon.com/cli/latest/reference/sns/subscribe.html
aws sns confirm-subscription --token 000 \ --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic \ --region us-west-2To confirm a Subscription and enable Authenticate on Unsubscribe:
aws sns confirm-subscription --token 000 \ --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic \ --authenticate-on-unsubscribe true \ --region us-east-2https://docs.aws.amazon.com/cli/latest/reference/sns/confirm-subscription.html
When you have one or more subscribers, you can return to your Topic and publish a test message.
aws sns publish --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic \ --message "This is a test message." \ --subject "Hello, World!" \ --region us-west-2https://docs.aws.amazon.com/cli/latest/reference/sns/publish.html
Now we can make our SNS workflow more realistic by automating the publishing of our messages. Since SNS is often used as internal messaging for distributed applications, we will simulate our scenario using CloudWatch Alarms. That tool allows us to monitor various metrics from other AWS services, and among other things, it supports delivery of notifications to SNS. For this demonstration, our alarm will be triggered by the invocation of any Lambda function in our account. The following process will be done exclusively through the web console.
You can now trigger your alert. If your configuration matches mine, you can invoke a Lambda function. You can do this by opening up the function and pressing the Test button.
Wait for the period of time specified in your CloudWatch duration metric, which is five minutes by default. You should then receive your email, the contents of which are automated.
When you are done testing you can end remove the alert by returning to CloudWatch Alarms, clicking the name of your alarm, and either deleting it or editing the conditions.