Broadcasting Email Notifications Using Amazon SNS

Aishwarya Kudalkar
6 min readJun 25, 2021

--

Nowadays most applications require instant messages to be sent to a large group of subscribers. Push notifications and emails are both powerful ways to seamlessly interact with your users. These notifications can be a way to draw more attention as these contain only essential information that influences the user directly.

Amazon AWS offers several services for delivering push/email notifications. One of them is AWS SNS. Amazon Simple Notification Service is a message publishing and processing service based on the Pub/Sub(Publish/Subscribe) model. This allows us to send texts, emails, push notifications, or other automated messages to other targets across multiple channels at the same time. Applications can use SNS to push real-time notifications to subscribers over multiple endpoints like Amazon SQS, AWS Lambda and HTTP endpoints

The pub/sub model allows to exchange messages asynchronously to many applications. In simple words, a publisher will send messages to a topic which will be consumed by a subscriber. The Pub/Sub model can be used to enable event-driven architectures or to decouple applications in order to increase scalability, performance and reliability.

Core concepts:

Topic: A logical access point to which publishers send messages to be relayed out to subscribers.

Message: Information or data that needs to be broadcasted to a number of subscribers.

Publisher: An application that will push the messages to a topic.

Subscriber: An application that will consume the messages from a topic that it will register to.

Now let’s have a look at how we can leverage Amazon SNS to send out email notifications. In the following sections, we will build a simple web app that can publish a message to multiple subscribers all at once.

Firstly, you will need to create an AWS account and an IAM Administrator user to work with. AWS SNS is based on the Pay as you go model and provides you with first 1000 email notifications for free every month.

Creating an IAM User

To work with Amazon SNS, you need the AmazonSNSFullAccess policy and AWS credentials that are associated with your IAM user. These credentials will also be used by the AWS JavaScript SDK to interact with AWS SNS.

  1. Open the Identity and Access Management.

2. Click on ‘Users’ on left navigation pane and then click on ‘Add Users’

3. Click on ‘Next: Permissions’ and attach the required policies.

4. Now click on ‘Next: Tags’ and then again on ‘Next: Review’ and then you’ll see a summary page.

5. Click on ‘Create User’ and your AmazonSNSUser is created. Make sure you download these credentials as these will not be available later and store them in a safe place. Do not publish on any public repositories.

Setting up an SNS Topic

To get started, lets first create a Topic to which notifications can be published.

  1. On your AWS dashboard, search for Simple Notification Service. Once you see the screen as below, select Topics from the left navigation pane.

2. You’ll be presented with a screen that lets you enter basic details for the topic. For this tutorial, we will be using a Standard topic. You could also use a FIFO topic if there is requirement of strict message ordering that needs to be followed.

At the bottom of the screen hit the Create button.

Creating subscriptions to the topic

Now let’s add some subscribers that will be receiving the email notifications.

1. On the AWS SNS dashboard select ‘Subscriptions’ from the left navigation pane and then click on ‘Create Subscription’

2. Enter the name of the topic that was created previously and you will see the Topic ARN in the drop-down.

3. For the protocol select Email from the drop-down list. Also add in an email address in the Endpoint.

4. Click on ‘Create Subscription’ button to complete this process.

5. After the subscription is created, a confirmation email will be sent to the Endpoint that was added. You must confirm it in order to be able to receive the email notifications.

Repeat the same procedure to add more subscribers.

Building a Node.JS App

For this tutorial, we will be building a simple Node.js application using express. This application will have single endpoint to send out emails.

  1. Firstly, let’s create a workspace and initialize the application.

On initializing the application, enter all the necessary details or you can skip by clicking enter. By default, the entry point will be index.js.

2. Next up is to install the necessary dependencies. We will need to install Express, aws-sdk and dotenv modules for this application.

3. Open this workspace in VS Code and start coding!
4. Lets create a .env file to store some environment variables. The following environment variables will be needed to establish a connection with Amazon SNS topic.

Do not publish the .env file on any public repositories. To avoid any mistakes add .env to .gitignore file.

5. Add the following lines of code to make sure we can interact with Amazon SNS.

  • Most of the code is boilerplate for Express.
  • Lines 4 and 5 requires the dotenv package and executes its config function, which reads the .env file and sets the environment variables.
  • Lines 7–10 requires the AWS SDK package, forms the credentials object to setup the connection with SNS.

6. Execute the program and then hit localhost:3000/status. If everything works fine then localhost:3000/status will display a JSON output.

7. Now lets create the endpoint that will be used to publish messages to our topic ‘notifyViaEmail’

To publish the message we are making use of 3 parameters.

  • Message: This will contain information that should be in the body of the email.
  • Subject: Subject line for the email notification.
  • TopicArn: Identifier for the topic that our application will publish messages to.

Take a look at the documentation to find more options available for the publish method.

8. Let’s test our application using Postman.

9. Once the request completes, all the subscriber endpoints will receive the email notifications.

Conclusion

Amazon Simple Notification Service is a fully managed service that provides developers with a highly scalable, reliable and cost-effective capability to publish messages from an application and immediately deliver them to subscribers. With simple APIs requiring minimal development effort, no maintenance or management overhead and pay-as-you-go pricing, Amazon SNS gives developers an easy mechanism to build a powerful notification system with their applications.

--

--