Simple Notification Service (SNS)
This demo implements a simple mailing list, which uses SNS as its backend.
Current subscriptions
Add subscription
| E-mail address: |
|---|
When you add a subscriber, AWS will automatically send a subscription e-mail to this e-mail address. The recipient needs to confirm subscription before the subscriber ARN is created and added to the topic.
Send message
| Subject: | |
|---|---|
| Body: |
Unsubscribing
All messages that are sent via e-mail include an "unsubscribe" link by default. This allows a user to unsubscribe without having to login to the AWS console, or accessing a web page, or whatever.
When a user unsubscribes via this link, the subscription will be marked as "Deleted". It will persist in this state for three days, after which AWS deletes the subscription completely. This allows the user to opt-in again, in case of regret. The opt-in link is included in the "Unsubscription confirmed" message.
Remove subscription manually
| ARN |
|---|
Some further details
This demo is written in JavaScript. Look at the source of this page, at the end of the document, to view the code. It is very straightforward.
All JavaScript routines use the same backend IAM user, SNSDemo. This user has the following policy attached:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sns:Subscribe",
"sns:Unsubscribe",
"sns:ListSubscriptionsByTopic",
"sns:Publish"
],
"Effect": "Allow",
"Resource": [
"arn:aws:sns:eu-central-1:973674585612:SNSDemo"
]
}
]
}
In a production environment, you may want to make a distinction between the user tasks (Subscribe, Unsubscribe), the management tasks (ListSubscriptions) and the sender tasks (Publish). Also it is considered bad practice to store the user account credentials (access key, secret key) in the code, like I've done here. In this particular case, the policy associated with this user is very limited so I'll take my chances. However, in a production environment you would be using something like AWS Cognito to provide credentials to the user. But that would make the demo a lot more complicated and would distract from its purpose.
This particular demo only uses/supports e-mail addresses. But SNS supports a lot of other endpoints as well, including:
- SMS notifications (Note that this is relatively costly: Between 10 and 15 cents for each SMS message delivered. And the "free tier" only applies to US telephone numbers.)
- AWS SQS
This is typically used in a fan-out scenario: A single message sent to an SNS topic will be sent to all SQS queues that are subscribed to this topic. Each SQS queue can then be used to queue different types of tasks. Demo here.
- Platform Applications: These allow for a message to be sent to the Apple Push Network (APN) or Google Cloud Messaging (GCM). This causes your phone to beep and a notification to be displayed on-screen, even if your phone is locked.
- AWS Lambda: For custom processing of messages