What are SNS, SQS, and Lambda, and how are they utilized?
Simple Notification Service (SNS), Simple Queue Service (SQS) and Lambda are powerful AWS services that can be used together to build scalable and reliable microservices-based applications.
SNS, SQS, and Lambda for Asynchronous Microservice Processing
SNS, SQS, and Lambda can be used to implement a fanout scenario in a microservices architecture. SNS is a publish-subscribe messaging service that allows you to decouple your microservices and send messages to them asynchronously. SQS is a message queuing service that provides a durable and reliable way to store and process messages.
Fanout scenario publishes a single message to an SNS topic and then distributed to multiple SQS queues. This allows you to process the message in parallel across multiple microservices, which can improve the scalability and performance of your application.
Example
E-commerce websites can improve the scalability and performance of their order processing systems by using Amazon SNS, SQS, and lambda (Simple Notification Service (SNS), Amazon Simple Queue Service (SQS), and AWS Lambda) to process bulk orders in parallel.
When a customer places a bulk order, the website publishes a message to an SNS topic. The message contains information about the order, such as the customer's name, address, and the products they ordered.
The SNS topic is subscribed to by multiple SQS queues, each of which is responsible for processing a different part of the order. For example, one queue might be responsible for sending an order confirmation email to the customer, another queue might be responsible for updating the inventory system, and another queue might be responsible for shipping the order.
The SQS queues are then processed by Lambda functions. Each Lambda function is responsible for performing a specific task, such as sending an email, updating the inventory, or shipping the order.
How to Use SNS, SQS, and Lambda for Asynchronous Microservice Processing
"Man who chooses to enjoy a pleasure that has no annoying consequences"
- • Open the Amazon SNS Topic console.
- • In the navigation pane, choose “Topics”.
- • On the “Topics” page, choose “Create topic” button.
- • Install terraform in your Linux terminal/ mac terminal/ command prompt.
- • Select “Standard” Option.
- • Enter the “Name” of SNS topic that describes your SNS service.
- • Click on “Create topic” button.
Repeat the above steps for additional SQS queues.
- • Click on “Create function” button.
- • After creating function go to function page and select the function that you created.
- • For SQS poll messages attach the SQS access permission in that function.
- • Select the tab “Configuration” and select the “Permissions” and click on “IAM role”.
- • After clicking on “IAM role” link navigate to “IAM Console” and select the tab “Permissions” and select “Add permissions” dropdown and click on “Attach policies”.
- • Search “AmazonSQSFullAccess” policy in search bar and select “AmazonSQSFullAccess” and then click on “Attach policies” button.
Open the Amazon SNS Topic console.
- • In the left-hand side navigation pane, click on “Topics” and select SNS topic that you created above.
- • For sending message click on “Publish message” button.
- • Write message subject in “Message detail” section and message detail in “Message body” section.
- • Click on “Publish message” button and send message to SQS.
For confirmation, the message was received in SQS OR not, Open the Amazon SQS Queue console.
Repeat the same Process for Checking another SQS Queue.
For reading that message details open the Lambda function console.
Glossary
SNS is a publish-subscribe messaging service that enables you to decouple microservices, distributed systems, and serverless applications. SNS publishes messages to topics, and subscribers receive messages from topics. SNS supports a variety of message delivery protocols, including HTTP, HTTPS, email, SMS, and mobile push notifications.
It is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. SQS offers a simple, durable, and highly scalable way to buffer messages between applications and components. SQS supports first-in-first-out (FIFO) delivery to ensure that messages are processed in the order that they were received.
Lambda is a serverless compute service that enables you to run code without provisioning or managing servers. Lambda triggers your code in response to events generated by AWS services, such as SNS notifications, SQS messages, and changes to DynamoDB tables. Lambda automatically scales your code based on demand, so you can focus on building applications without worrying about managing infrastructure.