AWS SQS and SNS Node JS tutorial: Integrating AWS SQS and SNS in a NestJS Application with TypeScript

0
961

In microservices architectures, AWS SQS and SNS are essential services for connecting and managing communication between distributed systems. In this blog post, we will guide you through setting up AWS SQS and AWS SNS in a NestJS project using TypeScript. You’ll also learn how to use the SNS fan-out pattern to broadcast messages and subscribe to those messages using SQS.

Additionally, you will get the full setup, including SQLite database integration to save processed messages and SQS polling to automatically process and manage messages from the queue.

SNS Fanout Pattern:

1.png?token-hash=36ZuBItxchJEgpp96tMCUPN_cjRkLYv_Re5RYNj9R5w%3D&token-time=1780704000

The SNS fan-out pattern is a mechanism through which a single message is sent to multiple subscribers. Instead of sending separate messages to each subscriber, a message is published to an SNS topic, and multiple subscribers consume it through SQS or other supported endpoints.

For example, consider a scenario where we have an Order Service, Invoice Service, and Order-Tracking Shipment Service. When a message is published to an SNS topic, such as order-created, by the Order Service, other services can listen to the message through SQS and begin processing it.

  • The Invoice Service will generate an invoice for the order.

  • The Order-Tracking Shipment Service will initiate order tracking and shipment.

This allows for efficient and decoupled message handling across multiple services.

Setting Up a NestJS Project with SQS and SNS.

Prerequisites

1. Basic understanding of NestJS.
2. An AWS account.
3. Familiarity with SQS (Simple Queue Service) and SNS (Simple Notification Service).
4. Node.js installed on your system.

1.png?token-hash=K-zlURT79Tm1hEq6OMBwLQeGijAmmJ-0K-B1NW5tYGU%3D&token-time=1780704000

Step 1: Setting Up NestJS

First, create a new NestJS project:
npx @nestjs/cli new nest--project
Follow the CLI prompts to complete the setup.

Step 2: Install Required Dependencies

1. Navigate to the project Directory

cd nest-project

2. Install the AWS SDK modules for SQS and SNS, along with other necessary packages:

npm install @aws-sdk/client-sns @aws-sdk/client-sqs

3. Install additional dependencies for SQS polling and database support:

npm install  sqs-consumer @nestjs/typeorm typeorm sqlite3

4. Install other dependencies 

npm install @nestjs/config uuid dotenv

Step 3: Configure Environment Variables

Create a .env file in the root of your project and add your AWS credentials:

AWS_ACCESS_KEY_ID=<your-access-key-id>

AWS_SECRET_ACCESS_KEY=<your-secret-access-key>

AWS_REGION=<your-region>

SNS_TOPIC_ARN=<your-sns-topic-arn>

SQS_QUEUE_URL=<your-sqs-queue-url>

Load these variables globally using NestJS's ConfigModule, and update imports in app.module.ts

1.png?token-hash=6a-HmQ5mk-4QZAYGYBi9A7y_XgZTD91eTDRUYt9ejyM%3D&token-time=1780704000

Step 4: Set Up SQS in AWS

1. Create an SQS Queue:

1. Log in to the AWS Management Console.

2. Navigate to the SQS service.

3. Click on Create Queue.

4. Choose either a Standard or FIFO queue based on your needs.

5. Configure the queue settings (e.g., name, visibility timeout).

6. Click Create Queue.

For more details, refer to the Amazon SQS Documentation

Step 5: Set Up SNS in AWS

1. Create an SNS Topic:

1. Navigate to the SNS service in the AWS Management Console.

2. Click on Create topic.

3. Choose either a Standard or FIFO topic based on your requirements.

4. Enter a name for your topic and configure additional settings.

5. Click Create topic.

2. Subscribe to the SQS Queue to the SNS Topic:

1. Navigate to your SNS topic.

2. Click Create Subscription.

3. Choose Amazon SQS as the protocol.

4. Enter the ARN of your SQS queue.

5. Click Create Subscription.

6. Add permissions to allow SNS to publish to your SQS queue.

For more details on SNS, check out the Amazon SNS Documentation.

1.png?token-hash=M1trbDodkVjiTxCHRj96qZGR5IQN_al7Svy2tssRXxM%3D&token-time=1780704000

Step 6: Configure SQLite database in NestJS:

1. Create the SqsMessage Entity:

Define the SqsMessage entity in sqs/models/sqs-message.ts:

Site içinde arama yapın
Kategoriler
Read More
Oyunlar
Harry Potter Goblet of Fire Illustrations – New Art Revealed
Harry Potter Goblet of Fire Illustrations Bloomsbury and Scholastic have just revealed a blazing...
By jiabinxu80 2025-10-08 00:37:37 0 1K
Networking
Silk Market Is Blending Luxury Fashion with Sustainable Textile Innovation
According to the latest report published by Data Bridge Market Research, the Silk...
By kshdbmr 2026-05-26 07:45:52 0 296
Shopping
Caratbazaar-Sustainable and Stylish: Why Lab-Grown Diamonds Are Gaining Popularity
In the past, diamonds were only thought of as those shiny stones pulled out from deep mines,...
By caratbazaar 2025-10-07 12:14:49 0 3K
Other
Strategic Advantage of Microsoft Office 365 Licenses Beyond Basic Productivity
The selection of software suite is a fundamental business decision in the modern digitally-driven...
By litsservices 2025-12-16 04:50:51 0 2K
Oyunlar
Harry Potter China Release: Warner Bros. Accelerates Plans
After hitting theaters in the U.S. on Nov. 16, pirated videodisc copies of Harry Potter and the...
By jiabinxu80 2025-10-25 00:44:59 0 1K
TagInTime - Privacy-First Social Network https://tagintime.com