Enabling Email Subscriptions With AWS SNS

What is Amazon SNS ? and What is Enabling Email Subscriptions With AWS SNS?

Amazon Simple Notification Service (Amazon SNS) is a managed service that provides message delivery from publishers to subscribers (also known as producers and consumers). Enabling Email Subscriptions With AWS SNS is to allow us to send messages from an SNS topic directly to email addresses whenever a message appears on the topic

Understanding AWS SNS

With AWS SNS, you can send messages to a range of endpoints via a fully managed messaging service. It simplifies the message delivery process by decoupling the sender from the receiver, allowing for greater scalability and reliability. You can use SNS to deliver alerts, notifications, and other messages to a variety of subscribers, including those with email addresses, SMS, push notifications on mobile devices, and more.

We can send messages from an SNS topic directly to email addresses whenever a message appears on the topic by setting up email subscriptions to the topic. It’s an easy and efficient way to get updates, alerts, and any other information published on an SNS topic.
Every message published to the SNS topic is sent as an email to the subscribed email address after we do so using the AWS Console, AWS CLI, or SDKs.
Email subscriptions are frequently used in applications to provide alerting and monitoring features.

In this hands-on, we will create an email subscription to an SNS topic. We will follow the process of confirming the email subscription. We will then send test messages to the SNS topic and will test whether they are delivered successfully to the subscribed email addres

Objective for Enabling Email Subscriptions With AWS SNS

  • Create a standard topic and subscribe an email to it
  • Confirm the email subscription
  • Test the end-to-end flow by publishing messages to the SNS topic and checking them in the email inbox

Sign in to the AWS Management Console
Navigate to the AWS Management Console (https://console.aws.amazon.com/) and sign in with your AWS account credentials.
Open the SNS Service
Once signed in, navigate to the Amazon SNS service

Standard Topics

Standard topics are ones that do not follow the order of the messages. Messages are not sent to subscribers in the same order as they were published on the topic. In other words, standard topics do not guarantee message order. There is no assurance that the sequence in which you get your 10 messages—ID1 through ID10—will match the one that was published (ID1, ID2, ID3, etc.). ID2 might arrive before ID1.

Creating a Demo Standard topic

Invoke the create-topic method on the sns service

#creating a demo topic
aws sns create-topic --name demo-topic
{
    "TopicArn": "arn:aws:sns:us-east-1:715471864368:demo-topic"
}

TopicArn is the unique identifier of the topic, which is formed by concatenating region, account, and topic name

Creating a Standard Topic for hands-on

Store the ARN in a local variable (TOPIC_ARN). We can use the query attribute to extract the TopicArn attribute

TOPIC_ARN=`aws sns create-topic \
>     --name my-standard-topic \
>     --output text \
>     --query TopicArn`
$ 
$ echo "Standard topic with ARN $TOPIC_ARN created successfully"
Standard topic with ARN arn:aws:sns:us-east-1:877555167645:my-standard-topic created successfully

Want to learn Thumbnail Creation Demo Using AWS Lambda ?

Fetching the User’s Email Address

We will need the email address of the user (your email address) so we can enable the subscription via email.

#!/bin/bash
read -p "Please enter your email address: " EMAIL_ADDRESS

When executed, this command will ask you to enter your email address at the command prompt. Once the email is supplied, it will store the value in a local variable called EMAIL_ADDRESS.

Just to be sure we’ve captured the email address, echo the variable:

echo $EMAIL_ADDRESS

Should this return the email address you’ve entered, we can move forward. If not, you may have to retry the above command so the email address is captured in the local variable EMAIL_ADDRESS.

Subscribing via Email

We created a topic earlier. A message published to this topic can be consumed by a subscriber. Subscribers can be of a few types, including an email subscriber, an SMS subscriber, an HTTP endpoint, an SQS queue, and so on.

Since we are working with an email subscription in this hands-on, we will subscribe our personal email to this topic.

To do so, we will invoke the subscribe method on the sns service:

aws sns subscribe --topic-arn $TOPIC_ARN --protocol email --notification-endpoint $EMAIL_ADDRESS

Confirm the Email Subscription

When we subscribe our email to a topic, SNS will send out a confirmation email to the given email address. A sample confirmation email is shown in the following image

Click the link in the email to confirm the subscription. The link will open up a browser tab and confirm your subscription. You should see a message in the browser; something like this

Publishing Messages to a Topic

Publishing a test message to an SNS topic is straightforward. We invoke the publish method on the sns service, providing the topic ARN and the message

aws sns publish --topic-arn $TOPIC_ARN --message "All Is Well"

As we subscribed to the topic via email, the same message will be delivered to our email address.

Check your Email

Now that we’ve published the test message to my-standard-topic, the message has been sent out to the email address assigned as the subscriber.

2 thoughts on “Enabling Email Subscriptions With AWS SNS”

Leave a Comment

Your email address will not be published. Required fields are marked *