Developer Guide: Implementing Two-Way SMS with Fastify, Node.js, AWS Pinpoint, and SNS - code-examples -

Frequently Asked Questions

Use AWS Pinpoint to send outbound messages and configure a dedicated phone number. Incoming messages are routed through AWS SNS to a webhook on your application, enabling two-way communication. This setup allows your app to both send and receive SMS messages.
Fastify serves as a high-performance Node.js web framework for creating the backend application. Its speed and efficiency make it ideal for handling API requests and serverless functions within the two-way SMS architecture.
AWS Lambda provides serverless compute, allowing you to run your Fastify application without managing servers. This offers scalability and cost-efficiency, as you only pay for the compute time used to process messages.
While this guide uses AWS SDK v2, AWS SDK v3 is recommended for new projects due to its modularity and modern features. Migrating to v3 requires adjustments to client initialization and API call syntax.
Yes, you can run the application locally and test outbound SMS sending using tools like curl or Postman and the /send endpoint. Testing inbound messages locally is more complex, requiring tools like ngrok to simulate SNS notifications, with full end-to-end testing often being easier after deployment.
The core AWS services are Pinpoint for sending, SNS for receiving, Lambda for running the application, API Gateway for the HTTP endpoint, and IAM for permissions. An AWS account with necessary permissions is a prerequisite.
Inbound SMS messages are routed from the user's phone to your Pinpoint number, which then triggers an SNS notification to your application's webhook. The application processes the message, enabling actions like auto-replies.
For local development, store credentials securely in a .env file (never commit to version control). For the deployed Lambda function, use an IAM Execution Role to grant the necessary permissions, avoiding the need to embed credentials directly in the function's environment.
API Gateway creates an HTTP endpoint that serves as the entry point for sending outbound SMS messages (via the /send route) and receiving inbound messages via SNS (via the /webhook/sns route). This enables communication between external systems and your Lambda function.
Create an SNS topic and link it as the incoming message destination in your Pinpoint phone number configuration. This ensures all messages sent to your Pinpoint number are published to this SNS topic, which will then forward them to your Fastify application.
Node.js version 18 or later is recommended for this project. This ensures compatibility with the latest features and dependencies used in the tutorial.
The Fastify application, triggered by an inbound SMS message via SNS, contains logic to process the message and generate an automatic reply. This is demonstrated with a simple example in the provided code.
SNS message signature validation is crucial for security. It prevents Server-Side Request Forgery (SSRF) attacks by verifying that incoming messages genuinely originate from SNS. Libraries like sns-validator are recommended for this purpose.
Package the application code and dependencies into a zip file. Create a Lambda function, configure its execution role with necessary permissions, upload the zip file, set the handler to src/lambda.handler, and configure the required environment variables.