Build Production-Ready Two-Way SMS with NestJS and AWS - code-examples -

Frequently Asked Questions

Set up two-way SMS by configuring AWS services and a NestJS application. This involves linking an AWS phone number to an SNS topic, subscribing an SQS queue to the topic, and triggering a Lambda function containing the NestJS app to process messages from the queue. This architecture allows for receiving and sending SMS messages.
Amazon Pinpoint provisions phone numbers and manages SMS communication, including two-way messaging. It acts as the entry point for inbound SMS messages, forwarding them to SNS. Pinpoint's "SMS and Voice" console section is key for this setup.
SQS decouples the SMS receiving (SNS) and processing (Lambda/NestJS) parts of the system. This adds resilience by buffering messages and enabling retries if the Lambda function is unavailable. It also allows the system to handle spikes in incoming SMS traffic.
Enable raw message delivery when you want the SQS message body to directly contain the original SMS payload from Pinpoint. This simplifies parsing in your Lambda function by removing the extra SNS message wrapping. The code examples provided in the article assume raw delivery is enabled.
Yes, you can send replies using the AWS SDK for JavaScript v3 within your NestJS application. The `SmsService` example demonstrates using the `PinpointSMSVoiceV2Client` to send outbound text messages via the `SendTextMessageCommand`. Ensure your Lambda execution role has the necessary `pinpoint-sms-voice:SendTextMessage` permission.
Incoming SMS messages are handled by a controller in your NestJS application. Use the `@EventPattern` decorator with a matching pattern in your `main.ts` file to route incoming SQS messages to the correct controller method. This method then processes the message content (e.g., parsing keywords) and executes relevant logic.
Prerequisites include an AWS account with necessary permissions, Node.js and npm/yarn, NestJS CLI, AWS CLI, a provisioned phone number in Amazon Pinpoint, and basic understanding of TypeScript and NestJS. Familiarity with Serverless Framework or AWS CDK is recommended for deployment.
The custom transport strategy allows the Lambda handler to find the correct NestJS message handler based on the content or pattern of the incoming SQS message. It connects the Lambda event trigger to the @EventPattern decorators in your NestJS controller, mimicking microservice event handling.
Review the permissions of your IAM roles and policies, particularly for Pinpoint, SNS, SQS, and Lambda. Verify that the Pinpoint role can publish to SNS, the SNS topic allows the Pinpoint service, SQS allows messages from SNS, and the Lambda role can access SQS, CloudWatch Logs, and Pinpoint for sending replies.
Opt-out handling is crucial for compliance. You can implement custom logic in your NestJS application to handle keywords like "STOP" to unsubscribe users, likely updating your database or another system accordingly. AWS Pinpoint might also offer automatic opt-out handling based on region and number type.
The architecture uses Amazon Pinpoint (for phone numbers and SMS/MMS), SNS (for notifications), SQS (for queuing), Lambda (for serverless compute), and the AWS SDK for JavaScript. IAM roles and policies are used for permissions management, and CloudWatch for monitoring and logging.
Core technologies include NestJS (backend framework), Amazon Pinpoint (SMS management), SNS (messaging), SQS (queuing), AWS Lambda (serverless compute), and the AWS SDK for JavaScript v3. Serverless Framework or AWS CDK are recommended for infrastructure management.