Create a notification alert system with AWS SNS, DynamoDB and AWS Lambda

Harsha Siriwardana
7 min readDec 15, 2019

If you are a person working close to the stock exchange, or regularly working with foreign currency, then currency conversion rates to your local currency is something you keep eyes on a daily basis.

I had a similar requirement and created this notification alert system. Normally I want to know the conversion rates to GBP(U.K. Pound) to LKR(SriLankan Rupees) daily basis based on the daily rates given by the respective bank. Rates differ from one bank to another.

This is part one of the series of tutorials on how to build this application. we are building a user interface to subscribe to the application as well later.

before diving into the application how works, it is good to have a basic idea of AWS features we are using for this application.

AWS Services

If you are new to AWS, there is the official AWS Getting Started portal. You will need access to an AWS account. You can signup for the AWS free tier.

AWS Lambda

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second.

The advantage of using AWS Lambda is that you don’t need to worry about managing it like EC2 instances and it helps to build serverless applications.

You can use AWS Lambda to run your code in response to events, such as changes to data in an Amazon S3 bucket or an Amazon DynamoDB table, etc. We will use DynamoDB table updates as a trigger to run lambda in here. Also, it is capable of running the scheduled time intervals.

Refer to this article for more information about AWS Lambda.

DynamoDB

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. DynamoDB lets you offload the administrative burdens of operating and scaling a distributed database so that you don’t have to worry about hardware provisioning, setup, and configuration.

Refer to this article for more information about AWS DynamoDB.Amazon Simple Notification Service

Amazon SNS (Simple Notification Service)

Amazon Simple Notification Service (SNS) is a highly available, durable, secure, fully managed pub/sub messaging service that enables you to decouple microservices, distributed systems, and serverless applications. Amazon SNS provides topics for high-throughput, push-based, many-to-many messaging.

Using Amazon SNS topics, your publisher systems can fan-out messages to a large number of subscriber endpoints for parallel processing, including Amazon SQS queues, AWS Lambda functions. Additionally, SNS can be used to fan out notifications to end users using mobile push, SMS, and email.

We are using this fan out notifications in this application.

There are few terminologies we need to familiar related with SNS.

Topics are named groups of events or access points, each identifying a specific subject, content, or event type. Each topic has a unique identifier (URI) that identifies the SNS endpoint for publishing and subscribing.

Owners create topics and control all access to the topic. The owner can define the permissions for all of the topics that they own.

Subscribers are clients (applications, end-users, servers, or other devices) that want to receive notifications on specific topics of interest to them.

Publishers send messages to topics. SNS matches the topic with the list of subscribers interested in the topic and delivers the message to each and every one of them. Here’s how it all fits together.

Refer to this article to learn more about Amazon SNS.

Application structure & behavior

  1. We will send notifications to users via email and SMS. so there are two topics created in AWS SNS.
  2. Users can subscribe to a specific topic based on what method they want to deliver notifications.
  3. We will create a user interface to subscribe to a topic. We saved user data in dynamoDB while they are registering. Once database updates, it triggers the lambda function and it will create subscribes under the relevant topic.
  4. There is another scheduled lambda function running once day which will collect the data from the respective bank's daily exchange rates and published a message to topics.

Now you have a basic idea of what is the goal of this application and how it will work. So let’s dive into the implementation

Initial setup

  1. creates the topics. In this application, there are two topics for email and one for SMS. Copy the resource ARNs (Amazon Resource Names) as well since we need them later.
create topics

2. Create dynamoDB tables to store user data. We will create 3 tables in the database.

  • Banks — Stores the bank names. Which will be use full when we create a front end for registering.
  • UserDetails — This table is saved the data such as the user's preferred type of notification, phone number, email and user names, etc.
  • ExRateSubscription- This table is just the same as UserDetails table’s structure. but this table uses as a trigger to the lambda function. Once new data is added it will trigger and the user will be subscribed into topic and record will be removed from the table.
dynamoDB tables

Now we are left with creating lambda functions. Let’s look into this.

We can use different languages to write scripts for lambda, but I use python in this scenario. AWS has a library called boto3 which supports communication with AWS resources.

There are two lambda functions we need to create.

  • For subscribe to relevant topics when dynamoDB table updates
  • Generate messages and published to topic daily

Subscribe to topics

Create a lambda function and we need to add a trigger to DynamoDB table change. (In this example, for ExRateSubscription table changes)

Also, add the scheduled time to run the function apart from database changes. The reason for this will understand once you go through the python script.

Then we need to upload the python script as a deployment zip file into AWS Lambda. ( Will explain later)

Publishes to topics

Create another lambda function and we need to schedule it to run daily at a given time.

We use cron jobs to schedule the time intervals.

Follow the “Schedule Expressions Using Rate or Cron” article for more information.

Check this quick video of how to add a lambda function

Two things to highlight in here are

  1. Add proper timeout to function. by default, it has 3 seconds. depending on how heavy the function work we need to adjust the time.
  2. When creating the function, add role with necessary permissions.

Once we create two lambda functions, we are good to go. But we skip a very important area on how to deploy python scripts to AWS and how each script will work.

Now let’s focus on how to write scripts to handle our needs and deploy them in AWS Lambda function.

I use python as a scripting language but you can use whatever you are most comfortable with. BTW amazon has a good python library for AWS resources called boto3

Before that let’s see how to upload your code to the lambda function.

How to Upload code as a package

You can write a python script that does your pre-defined tasks. It should have a function, let’s call it “lambda_handler”. Your lambda console handler name should be set accordingly combined with filename and method name

create a package as a zip file and upload it to the lambda function. Follow this article on how to create a zip file with adding dependencies.

Python Code

We need to write python code for subscribing users for relevant topics when they register. Here is a sample code for that.

Also, we need another script that will read the respective bank’s exchange rates and publish them to topics.

Before studying the code, we use a python library called BeautifulSoap for parsing HTML and XML documents. We use python library called requests to read the web page content and use BeautifulSoap to parse HTML data.

A good tutorial about BeautifulSoap can be found here

Once you deploy your lambda functions it will subscribe That’susers to topics and those users can get notifications.

Here is the result.

daily email alert
daily email with exchange rate
Alert as SMS message

So this is how I use AWS services to get the daily notification to your email or phone about exchange rates.

Done and Dusted

Thanks for reading! (A few 👏 are always appreciated. 😀)

--

--