In today’s world, lots of apps are shifting to real-time. The IT world aims to get info to users ASAP, helping them stay updated for quick decision-making. Think apps for airplane details or live football scores. What’s the basic setup for these kinds of apps?
Keep in mind that the basic architecture is split into three main parts: the sender of messages or information, the place where messages are stored (the topic), and the entity that uses or consumes those messages.
To get a clearer picture, let’s take a look at the image below:
Cloud providers have developed services to make this pub/sub feature possible. Today, we’re diving into AWS, and specifically, we’ll discuss AWS Simple Notification Service (SNS).
Let’s start by defining AWS SNS. It’s a fully managed messaging service that allows for the creation and distribution of messages to a distributed group of recipients or endpoints.
AWS SNS offers two notification types: A2A (application to application) and A2P (application to person). - A2A involves communication between two software applications. - A2P covers interactions between applications and individuals (persons), including notifications, alerts, and marketing messages.
Now, let’s delve into some use cases of SNS:
Application Integration : Fanout Scenario : This occurs when a message in an SNS topic is replicated to multiple endpoints. These endpoints can include Kinesis Data Firehose delivery streams, Amazon SQS queues, HTTP(S) endpoints, and Lambda functions.
Application Alerts : if you define a specific threshold and want to trigger an event when that threshold is reached, you can use AWS SNS to send emails. This proves to be very useful for alerting in applications based on predefined conditions or thresholds you’ve set .
In our tutorial, we’ll build an e-commerce confirmation message system using an AWS SNS topic. Picture it as a website that sends an email or an SMS to a customer after they’ve made a purchase. To handle this, we’ll subscribe to the SNS topic using an AWS Lambda function. This setup allows for efficient communication and confirmation in an e-commerce scenario.
Requirements :
Navigate to the AWS Management Console, and access the SNS service.
Now, it’s time to create an AWS Lambda function that processes the data coming from the SNS topic.
It’s a straightforward process — just follow the UI, give the Lambda function a name, and leave the default values as they are. It should be created without any issues.
Step 3 : subscribe the lambda function to the SNS topic
When you click on the “Add” button, you’ll notice in the configuration section that a resource-based policy has been created. If you’re not familiar with what a resource-based policy is, no worries — let’s break it down together.
Imagine you have a treasure chest (AWS resource), and you want to decide who gets to open it and take what’s inside. The resource-based policy is like a set of rules you attach to the treasure chest to say who is allowed to open it and what they are allowed to take.
Open the Lambda function UI and navigate to the “Test” tab. In the template dropdown list, select our created SNS topic. Upon doing so, you’ll see an output similar to this .
we go to the code part and we will write javascript code in order to extract the events from SNS .
exports.handler = async (event) => { console.log("event:", JSON.stringify(event, undefined, 2)); var message = event.Records[0].Sns.Message; console.log('Message received from SNS:', message); };
Sure, let’s break it down for better understanding :
The first line exports a function named handler. In the context of AWS Lambda, the handler function is the entry point for the Lambda execution. It is triggered when an event occurs that is configured to invoke this Lambda function
handler
exports.handler = async (event) => {
event
console.log("event:", JSON.stringify(event, undefined, 2));
Records
Sns.Message
var message = event.Records[0].Sns.Message;
message
console.log('Message received from SNS:', message);
we will go to the SNS topic in the UI and we will click on the publish message button .
we will write our first message :
Now, click on the “Publish message” button.
Navigate to the Lambda UI, and in the “Monitor” section, click on “View CloudWatch logs.” You’ll observe the log streams displayed like this:
Click on it, and you should see our message displayed like this:
In our article today, we dived into some AWS tools for working with data. The crucial point is to really grasp the ideas and understand how the different parts of the system fit together. Doing this helps you feel confident about working in any setup with any tool.