Amazon Simple Notification Service
Amazon Simple Notification Service (SNS) is a web service which helps you easily publish and deliver notifications to a variety of end points in an automated and low cost fashion. SNS currently supports sending messages to Email, SMS, HTTP/S, and SQS Queue endpoints.
The moving parts
SNS is composed of three main parts
- A topic
- A subscription
- Published messages
A topic is a communication channel to send messages and subscribe to notifications. Once you create a topic, you're provided with a topic ARN (Amazon Resource Name), which you'll use for subscriptions and publishing messages.
A subscription is done to a specific endpoint of a topic. This can be a web service, an email address or an SQS queue.
Published messages are generated by publishers, which can be scripts calling the SNS API, users using the AWS console, or using the CLI tools. Once a new message is published, Amazon SNS attempts to deliver that message to every endpoint that is subscribed to the topic.
SNS has a number of cost factors, including API requests, notifications to HTTP/S, notifications to Email, notifications to SMS, and data transferred out of a region.
You can get started using SNS with AWS's Free Usage tier though. So you won't have to pay to play right away.
To get started with using SNS, we'll walk through making a topic, creating an email subscription, and publishing a message, all through the AWS console.
Making a topic
- Login to the AWS Console
- Click Create New Topic.
- Enter a topic name in the Topic Name field.
- Click Create Topic.
- Copy the Topic ARN for the next task.
You're now ready to make a subscription.
Creating an email subscription
- In the AWS Console click on My Subscriptions
- Click the Create New Subscription button.
- In the Topic ARN field, paste the topic ARN you created in the previous task, for example:
- Select Email in the Protocol drop-down box.
- Enter your email address for the notification in the Endpoint field.
- Click Subscribe.
- Go to your email and open the message from AWS Notifications, and then click the link to confirm your subscription.
- You should see a confirmation response from SNS
You're now ready to publish a message.
Publishing a message
- In the AWS Console click the topic you want to publish to, under My Topics in the Navigation pane.
- Click the Publish to Topic button.
- Enter a subject line for your message in the Subject field.
- Enter a brief message in the Message field.
- Click Publish Message.
- A confirmation dialog box will appear, click Close to close the confirmation dialog box.
- You should get the email shortly.
The SNS documentation has more details on:
- Create Different Messages for Each Protocol
- Managing Permissions on your SNS Topics
- Sending/Recieving SMS Notifications
- Using SQS Queues with SNS
- Sending Messages to HTTP/S Endpoints
- Message and JSON Formats
You've learned how to manually work with SNS, but as with all AWS services, things are best when automated.
Making a topic
This script will connect to us-west-2 and create a topic named adventtopic.
If the topic is successfully created, it will return the topic ARN. Otherwise, it will log any errors to sns-topic.log.
Making an email subscription
This script will connect to us-west-2 and create an email subscription to the topic named adventtopic for the email address you specify.
If the subscription is successfully created, it will return the topic ARN. Otherwise, it will log any errors to sns-topic.log.
- Note: You'll need to manually confirm the subscription in your email client before you can move on to using the pre for publishing a message.
Publishing a message
This script will connect to us-west-2 and create a message to the topic named adventtopic with the subject and message body you specify.
If the publication is successfully performed, it will return the topic ARN. Otherwise, it will log any errors to sns-publish.log.
At this point you've successfully automated the main use cases for SNS.
As you can see, SNS can be very useful for sending notifications and with a little automation, can quickly become a part of your application infrastructure toolkit.
All the code samples are available in the Github repository.