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

0
481

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:

البحث
الأقسام
إقرأ المزيد
الألعاب
Deluxe Adventurer Chest – Last War Survival Rewards
In the intense world of Last War Survival, securing powerful equipment is crucial for staying...
بواسطة jiabinxu80 2025-11-18 00:25:47 0 520
Networking
JNR Alien 10000 : Un seul appareil, 10 000 bouffées de pure puissance de vape
Introduction Les vapes jetables à grande capacité deviennent le choix...
بواسطة ainastarc 2026-02-23 12:14:08 0 28كيلو بايت
Sports
What Is Torque in a Golf Shaft (And Why It Matters for Your Game)
uring the swing. As you transition from backswing to downswing and approach impact, forces cause...
بواسطة steadfastgolf 2026-03-17 06:55:20 0 3كيلو بايت
أخرى
Lubricant Packaging Market Size, Share, Trends, Key Drivers, Demand and Opportunity Analysis
Lubricant Packaging Market – Global Insights, Trends, and Future Outlook 1....
بواسطة kajal 2025-10-09 08:04:04 0 2كيلو بايت
الألعاب
K-pop Holiday Celebration: Thanksgiving with Huntr/X
K-pop Holiday Celebration Give thanks for Huntr/X's epic year by blending K-pop magic into your...
بواسطة jiabinxu80 2025-11-25 02:44:44 0 466
TagInTime - Privacy-First Social Network https://tagintime.com