Developer Guide: Implementing Two-Way SMS with Fastify, Node.js, AWS Pinpoint, and SNS
A comprehensive guide to building a serverless two-way SMS system using Fastify, Node.js, AWS Pinpoint, SNS, Lambda, and API Gateway.
Code examples21 min read
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.
One API. Every channel.
Contact Sales
This guide provides a complete walkthrough for building a production-ready two-way SMS messaging system using Node.js with the Fastify framework, AWS Pinpoint for sending messages, and AWS Simple Notification Service (SNS) for receiving incoming replies. We will build a Fastify application deployable as an AWS Lambda function, triggered by API Gateway, capable of sending SMS messages via an API endpoint and automatically handling replies received via SNS.
By the end of this tutorial, you will have a scalable serverless application that can:
Project Overview and Goals
Problem: Businesses often need to send notifications, alerts, or marketing messages via SMS (Application-to-Person, A2P) and handle replies from users (two-way communication). Building a reliable and scalable system for this requires integrating messaging providers and handling asynchronous incoming messages.
Solution: We will leverage AWS services for robust messaging capabilities and Fastify for a high-performance Node.js backend, deployed serverlessly for scalability and cost-efficiency.
Technologies Used:
System Architecture:
Prerequisites:
curlor Postman).1. Setting up the Project
Let's initialize our Node.js project and install the necessary dependencies.
Create Project Directory:
mkdir fastify-sms-two-way cd fastify-sms-two-wayInitialize Node.js Project:
Install Dependencies:
fastify: The core web framework.aws-sdk: AWS SDK for JavaScript (v2) to interact with Pinpoint and SNS.dotenv: To load environment variables from a.envfile for local development.@fastify/aws-lambda: Adapter to run Fastify on AWS Lambda.Note: This guide uses AWS SDK v2. For new projects, AWS SDK v3 is generally recommended due to its modularity and modern features like middleware support. Using v3 would require changes to the AWS client initialization and API call syntax (e.g., importing specific client commands instead of the full SDK).
Create Project Structure:
Create the
srcandsrc/utilsdirectories.Create
.gitignore: Create a.gitignorefile in the project root to prevent committing sensitive files and dependencies:Create
.env.example: Create a.env.examplefile in the project root. This serves as a template. We will populate the actual.envfile later with credentials obtained from AWS.Security Note: The
.envfile containing actual secrets should never be committed to version control.2. AWS Configuration
Before writing code, we need to configure the necessary AWS resources.
Configure IAM Permissions (User for Local Dev, Role for Lambda):
IAM User (for Local Development/CLI): While an IAM user with access keys is needed for local development and interacting with AWS via the CLI or SDKs from your machine, the primary and recommended way for the deployed Lambda function to obtain permissions is through an IAM Execution Role (see Section 5.2). This avoids embedding long-lived credentials in the function's environment.
fastify-sms-dev-user).FullAccesspolicies, it is strongly recommended to create custom IAM policies granting only the necessary permissions for local testing. Examples include:pinpoint:SendMessages(to send SMS)sns:Publish,sns:Subscribe,sns:ListTopics(if needed for local SNS interaction/setup)iam:PassRoleif your local setup involves assuming roles.Access key IDandSecret access key. These will go into your local.envfile.IAM Role (for Lambda Execution): We will create or assign this role during Lambda deployment (Section 5.2). This role needs permissions like:
pinpoint:SendMessages(to send replies)logs:CreateLogGroup,logs:CreateLogStream,logs:PutLogEvents(for CloudWatch logging)sns:Publishif the Lambda needs to publish to other topics.Set up AWS Pinpoint:
+12065550100).Create an SNS Topic:
twoWaySMSHandler).arn:aws:sns:us-east-1:123456789012:twoWaySMSHandler.Link Pinpoint to SNS Topic:
twoWaySMSHandler) from the dropdown.Update
.envFile (for Local Development): Create a.envfile in your project root (copy.env.example) and populate it with the actual values you obtained for local testing:AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_REGION(from the IAM User created in Step 1).PINPOINT_APP_ID(from Pinpoint Project).PINPOINT_ORIGINATION_NUMBER(the Pinpoint phone number you acquired).SNS_TOPIC_ARN(from the SNS Topic).3. Implementing Core Functionality
Now let's write the code for our Fastify application.
Initialize AWS SDK Clients (
src/utils/aws.js): Create a utility file to centralize AWS SDK client initialization.// src/utils/aws.js 'use strict'; const AWS = require('aws-sdk'); // Using AWS SDK v2 // Load environment variables locally if not in Lambda if (!process.env.AWS_LAMBDA_FUNCTION_NAME) { require('dotenv').config(); console.log('Loaded .env file for local development.'); } // Basic validation for essential configuration const requiredConfig = { // Keys are env var names, values are descriptions AWS_REGION: 'AWS Region', PINPOINT_APP_ID: 'Pinpoint Application ID', PINPOINT_ORIGINATION_NUMBER: 'Pinpoint Origination Number', SNS_TOPIC_ARN: 'SNS Topic ARN for Inbound SMS' }; // For local dev, also check for credentials if (!process.env.AWS_LAMBDA_FUNCTION_NAME) { requiredConfig.AWS_ACCESS_KEY_ID = 'AWS Access Key ID (local only)'; requiredConfig.AWS_SECRET_ACCESS_KEY = 'AWS Secret Access Key (local only)'; } let configValid = true; for (const varName in requiredConfig) { if (!process.env[varName]) { console.error(`Error: Missing required environment variable: ${varName} (${requiredConfig[varName]})`); configValid = false; } } if (!configValid) { throw new Error(""Missing required environment variables. Check logs.""); } // Configure AWS SDK // In Lambda, credentials will be sourced from the execution role automatically if keys aren't set here. const awsConfig = { region: process.env.AWS_REGION, }; if (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) { awsConfig.accessKeyId = process.env.AWS_ACCESS_KEY_ID; awsConfig.secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY; console.log('Using AWS credentials from environment variables.'); } else if (process.env.AWS_LAMBDA_FUNCTION_NAME) { console.log('Using AWS credentials from Lambda execution role.'); } else { console.warn('Warning: AWS credentials not found in environment variables for local execution. SDK might try other sources (e.g., ~/.aws/credentials).'); } AWS.config.update(awsConfig); // Create Pinpoint client const pinpoint = new AWS.Pinpoint(); // Create SNS client (might be useful for other SNS operations, not strictly needed for receiving) const sns = new AWS.SNS({ apiVersion: '2010-03-31' }); console.log(`AWS SDK configured for region: ${process.env.AWS_REGION}`); console.log(`Using Pinpoint App ID: ${process.env.PINPOINT_APP_ID}`); console.log(`Using Pinpoint Origination Number: ${process.env.PINPOINT_ORIGINATION_NUMBER}`); module.exports = { pinpoint, sns, pinpointAppId: process.env.PINPOINT_APP_ID, pinpointOriginationNumber: process.env.PINPOINT_ORIGINATION_NUMBER, snsTopicArn: process.env.SNS_TOPIC_ARN, };dotenvonly locally..envlocally, relies on Lambda execution role when deployed.Create Fastify Application (
src/app.js): This file sets up the Fastify instance, defines routes for sending and receiving SMS, and includes basic error handling.// src/app.js 'use strict'; const Fastify = require('fastify'); const { pinpoint, pinpointAppId, pinpointOriginationNumber } = require('./utils/aws'); // --- Helper Function to Send SMS via Pinpoint --- async function sendSms(destinationNumber, message, messageType = 'TRANSACTIONAL') { if (!destinationNumber || !message) { throw new Error('Destination number and message are required.'); } // Basic E.164 format check (can be improved) if (!/^\+[1-9]\d{1,14}$/.test(destinationNumber)) { throw new Error('Invalid destination phone number format. Use E.164 format (e.g., +12065550100).'); } if (!pinpointOriginationNumber || !/^\+[1-9]\d{1,14}$/.test(pinpointOriginationNumber)) { console.error(`FATAL: Invalid or missing Pinpoint Origination Number configured: ${pinpointOriginationNumber}. Check environment variable PINPOINT_ORIGINATION_NUMBER.`); throw new Error('Server configuration error: Invalid origination number.'); } const params = { ApplicationId: pinpointAppId, MessageRequest: { Addresses: { [destinationNumber]: { ChannelType: 'SMS' } }, MessageConfiguration: { SMSMessage: { Body: message, MessageType: messageType, // TRANSACTIONAL (high reliability) or PROMOTIONAL (lower cost) OriginationNumber: pinpointOriginationNumber, // SenderId: 'MyBrand' // Optional: Custom Sender ID (requires registration in many countries) // Keyword: 'REGISTERED_KEYWORD' // Optional: Required by some carriers/regulations } } } }; try { const result = await pinpoint.sendMessages(params).promise(); const messageResult = result.MessageResponse.Result[destinationNumber]; // Check for delivery status - Pinpoint returns 200 OK even for some failures if (messageResult.DeliveryStatus !== 'SUCCESSFUL') { console.warn(`Pinpoint reported non-successful delivery status | To: ${destinationNumber} | Status: ${messageResult.StatusCode} - ${messageResult.DeliveryStatus} | Message: ${messageResult.StatusMessage}`); } else { console.log(`SMS Sent via Pinpoint | To: ${destinationNumber} | Status: ${messageResult.StatusCode} - ${messageResult.DeliveryStatus} | MessageID: ${messageResult.MessageId}`); } return messageResult; } catch (err) { console.error(`Error calling Pinpoint sendMessages API for ${destinationNumber}:`, err); throw new Error(`Failed to send SMS. AWS Error: ${err.message}`); // Re-throw specific error } } // --- Fastify Application Initialization --- function build(opts = {}) { const app = Fastify(opts); // --- Route: Send Outbound SMS --- // POST /send // Body: { ""to"": ""+1XXXXXXXXXX"", ""message"": ""Your message content"" } app.post('/send', { schema: { // Basic request validation body: { type: 'object', required: ['to', 'message'], properties: { to: { type: 'string', description: 'Recipient phone number in E.164 format', pattern: '^\\+[1-9]\\d{1,14}' }, message: { type: 'string', minLength: 1, description: 'SMS message content' } } }, response: { 200: { type: 'object', properties: { status: { type: 'string' }, deliveryStatus: { type: 'string' }, messageId: { type: 'string' }, destination: { type: 'string'} } }, // Add other response schemas for errors (400, 500) if desired } } }, async (request, reply) => { const { to, message } = request.body; request.log.info(`Received request to send SMS | To: ${to}`); // Use Fastify logger try { const result = await sendSms(to, message); // Use the helper function reply.code(200).send({ status: result.StatusMessage, // Pinpoint's status message deliveryStatus: result.DeliveryStatus, // Actual delivery status messageId: result.MessageId, destination: to }); } catch (error) { request.log.error({ err: error }, `Failed to send SMS to ${to}`); // Determine status code based on error type if (error.message.includes('Invalid destination phone number') || error.message.includes('required') || error.message.includes('pattern')) { reply.code(400).send({ error: 'Bad Request', message: error.message }); } else if (error.message.includes('Server configuration error')) { reply.code(500).send({ error: 'Configuration Error', message: 'Internal server configuration problem.' }); } else { reply.code(500).send({ error: 'Internal Server Error', message: 'Failed to send SMS.' }); } } }); // --- Route: Handle Inbound SMS from SNS --- // POST /webhook/sns // Body: SNS Notification containing the SMS message app.post('/webhook/sns', async (request, reply) => { request.log.info('Received webhook notification from SNS'); const messageType = request.headers['x-amz-sns-message-type']; try { // --- 1. Handle SNS Subscription Confirmation --- if (messageType === 'SubscriptionConfirmation') { request.log.info(`Received SNS Subscription Confirmation request. URL: ${request.body.SubscribeURL}`); // IMPORTANT: **NEVER** automatically visit the `SubscribeURL` in a production environment // without proper validation (e.g., checking the request signature). Doing so creates a // Server-Side Request Forgery (SSRF) vulnerability. // For this guide, we log the URL and rely on manual confirmation via the AWS console or browser. // Production systems should implement SNS message signature validation using libraries like `sns-validator`. console.log('ACTION REQUIRED: Confirm SNS subscription via the AWS SNS console or by manually visiting the logged SubscribeURL.'); reply.code(200).send({ status: 'Subscription confirmation required. Please confirm manually.' }); return; // Stop processing here } // --- 2. Handle SNS Notification Message --- if (messageType === 'Notification') { request.log.info('Processing SNS Notification...'); const snsMessage = request.body; // Already parsed by Fastify if Content-Type is application/json // The actual SMS data is within a JSON string inside snsMessage.Message if (typeof snsMessage.Message !== 'string') { request.log.error({ receivedMessage: snsMessage.Message }, 'SNS Message format unexpected. snsMessage.Message is not a string. Is Raw Message Delivery enabled?'); throw new Error('SNS Message format unexpected. Message is not a string.'); } let inboundSmsData; try { inboundSmsData = JSON.parse(snsMessage.Message); } catch (parseError) { request.log.error({ err: parseError, messageContent: snsMessage.Message }, 'Failed to parse JSON from snsMessage.Message'); throw new Error('Failed to parse inbound SMS data from SNS message.'); } request.log.info({ inboundSmsData }, 'Parsed inbound SMS data'); const senderNumber = inboundSmsData.originationNumber; const recipientNumber = inboundSmsData.destinationNumber; // Your Pinpoint number const messageBody = inboundSmsData.messageBody; // const messageId = inboundSmsData.messageId; // Available if needed // --- 3. Implement Business Logic (e.g., Auto-Reply) --- console.log(`Received SMS | From: ${senderNumber} | To: ${recipientNumber} | Message: ""${messageBody}""`); // Example: Simple Auto-Reply const replyMessage = `Thanks for your message! We received: ""${messageBody}"". We'll get back to you soon.`; try { await sendSms(senderNumber, replyMessage); // Send reply back to the sender console.log(`Auto-reply sent to ${senderNumber}`); } catch (replyError) { request.log.error({ err: replyError }, `Failed to send auto-reply to ${senderNumber}`); // Decide if the overall request should fail if auto-reply fails. // For now, we log the error but still return 200 OK for the webhook receipt. } reply.code(200).send({ status: 'Inbound message processed successfully' }); } else if (messageType === 'UnsubscribeConfirmation') { request.log.info(`Received SNS Unsubscribe Confirmation for Topic: ${request.body.TopicArn} | Token: ${request.body.Token}`); reply.code(200).send({ status: 'Unsubscribe notification received.'}); } else { request.log.warn(`Received unhandled SNS message type: ${messageType}`); reply.code(400).send({ error: 'Unhandled message type', type: messageType }); } } catch (error) { request.log.error({ err: error }, 'Error processing SNS webhook'); // Ensure a response is always sent to SNS to prevent unnecessary retries reply.code(500).send({ error: 'Internal Server Error', message: 'Failed to process incoming message.' }); } }); // --- Route: Health Check --- app.get('/health', async (request, reply) => { // Could add checks here (e.g., AWS connectivity) if needed return { status: 'ok', timestamp: new Date().toISOString() }; }); return app; } module.exports = { build, sendSms }; // Export build for lambda.js and sendSms for potential direct use/testingtonumber in/sendschema./sendroute based on error type.Create Lambda Handler (
src/lambda.js): This file uses@fastify/aws-lambdato wrap our Fastify application instance.// src/lambda.js 'use strict'; const awsLambdaFastify = require('@fastify/aws-lambda'); const { build } = require('./app'); // Import the build function from app.js // Initialize the Fastify app instance // Pass logger: true for CloudWatch logging integration const app = build({ logger: { level: process.env.LOG_LEVEL || 'info', // Control log level via env var // Pino default serializers handle common fields like err, req, res } }); // Create the proxy function using @fastify/aws-lambda // This adapts API Gateway v1 (REST) or v2 (HTTP) events to Fastify requests const proxy = awsLambdaFastify(app); // Export the handler function for AWS Lambda exports.handler = async (event, context) => { // Log basic event info (avoid logging full event in production if it contains sensitive data) console.log(`Lambda handler invoked. Request ID: ${context.awsRequestId}, Event source: ${event.requestContext?.domainName || 'Unknown'}`); try { // Add any invocation-specific context if needed, e.g., from context object // app.decorateRequest('lambdaContext', context); const result = await proxy(event, context); return result; } catch (error) { // Log the error using Fastify's logger if available, otherwise console.error if (app.log) { app.log.error({ err: error, event }, 'Error in Lambda handler proxy execution'); } else { console.error('Error in Lambda handler proxy execution:', error, 'Event:', event); } // Ensure a valid API Gateway response structure is returned on error return { statusCode: 500, body: JSON.stringify({ error: 'Internal Lambda Error', message: error.message || 'An unexpected error occurred.' }), headers: { 'Content-Type': 'application/json' }, }; } };@fastify/aws-lambdaproxy.4. Local Development and Testing
Before deploying, you can run the application locally.
Add Start Script: Add a script to your
package.jsonfor local execution:// package.json (add within ""scripts"") ""scripts"": { ""start:local"": ""node -r dotenv/config ./scripts/run-local.js"", // Create this script ""test"": ""echo \""Error: no test specified\"" && exit 1"" },-r dotenv/config: Preloadsdotenvto load variables from.env.Install
pino-prettyand Create Local Runner (scripts/run-local.js): First, install the development dependency for nice local logging:Now, create a
scriptsdirectory and addrun-local.js. This script runs the Fastify app directly using its built-in server.// scripts/run-local.js 'use strict'; // dotenv is loaded via -r flag in npm script const { build } = require('../src/app'); // Adjust path relative to project root const PORT = process.env.PORT || 3000; const HOST = '127.0.0.1'; // Listen only on localhost by default for security const start = async () => { const app = build({ logger: { level: 'info', transport: { // Pretty print logs locally using pino-pretty target: 'pino-pretty', options: { translateTime: 'HH:MM:ss Z', // Human-readable time ignore: 'pid,hostname', // Ignore noisy fields colorize: true, }, }, } }); try { await app.listen({ port: PORT, host: HOST }); // Logger is available after listen/ready event, but log.info works here too console.log(`Server listening on http://${HOST}:${PORT}`); console.log('Local routes:'); console.log(` POST http://${HOST}:${PORT}/send`); console.log(` POST http://${HOST}:${PORT}/webhook/sns`); console.log(` GET http://${HOST}:${PORT}/health`); } catch (err) { // Use console.error before logger might be fully initialized console.error('Error starting local server:', err); process.exit(1); } }; start();pino-prettyfor improved local log readability.app.listenfor standard HTTP server behavior.Run Locally: Make sure your
.envfile is populated with your IAM User credentials and other config.npm run start:localThe server should start, listening on port 3000.
Test Sending SMS (Local): Use
curlor Postman to send a POST request tohttp://localhost:3000/send:curl -X POST http://localhost:3000/send \ -H ""Content-Type: application/json"" \ -d '{ ""to"": ""+1RECIPIENTNUMBER"", ""message"": ""Hello from local Fastify!"" }'Replace
+1RECIPIENTNUMBERwith a valid test phone number. Check your terminal logs (nicely formatted bypino-pretty) and the recipient's phone for the message.Note on testing inbound: Testing the
/webhook/snsroute locally requires simulating an SNS notification. This is complex because AWS SNS needs a publicly accessible HTTPS endpoint to send notifications to. Tools likengrokcan expose your local server to the public internet with an HTTPS URL, which you could temporarily use in the SNS subscription. However, full end-to-end testing of the inbound flow is often easier after deployment.5. Deployment to AWS Lambda and API Gateway
We'll deploy the application as a Lambda function triggered by API Gateway.
Package the Application: Create a zip file containing your code and only production dependencies.
# Ensure you are in the fastify-sms-two-way root directory zip -r deployment_package.zip src node_modules package.json package-lock.jsonThis command packages the
srcdirectory (containingapp.js,lambda.js,utils/aws.js), the productionnode_modules, and package files.Create the Lambda Function:
fastify-sms-handler(or similar).x86_64orarm64.fastify-sms-handler-role-xxxx).pinpoint:SendMessages(to send replies from the webhook).logs:CreateLogGroup,logs:CreateLogStream,logs:PutLogEvents).Upload Code:
deployment_package.zipfile you created.Configure Handler and Environment Variables:
src/lambda.handler(pointing toexports.handlerinsrc/lambda.js).src/utils/aws.jswhen running in Lambda):AWS_REGION: Your AWS region (e.g.,us-east-1).PINPOINT_APP_ID: Your Pinpoint Application ID.PINPOINT_ORIGINATION_NUMBER: Your Pinpoint phone number (E.164 format).SNS_TOPIC_ARN: The ARN of your SNS topic for inbound messages.LOG_LEVEL(Optional): Set todebug,warn,errorto control logging verbosity (defaults toinfo).AWS_ACCESS_KEY_IDorAWS_SECRET_ACCESS_KEYhere. The Lambda function will use the permissions granted by its Execution Role.Create API Gateway (HTTP API): We'll use an HTTP API for simplicity and cost-effectiveness.
Lambda.fastify-sms-handlerfunction.SMSTwoWayAPI(or similar).POST, Resource path:/send. Integration target:fastify-sms-handler.POST, Resource path:/webhook/sns. Integration target:fastify-sms-handler.GET, Resource path:/health. Integration target:fastify-sms-handler.$defaultis fine for now. Ensure ""Auto-deploy"" is enabled.https://abcdef123.execute-api.us-east-1.amazonaws.com).Subscribe SNS Topic to API Gateway Endpoint:
twoWaySMSHandlertopic.HTTPS.https://abcdef123.execute-api.us-east-1.amazonaws.com/webhook/sns.JSON.parse(snsMessage.Message)). If unchecked, SNS wraps the message in its own JSON structure./webhook/snsroute). You should see the log message containing theSubscribeURLfrom our Fastify app. Manually copy and paste this URL into your browser to confirm the subscription. Alternatively, you can confirm it within the SNS console if the endpoint responds correctly (which our code does by logging and returning 200). Once confirmed, the status will change to ""Confirmed"".6. End-to-End Testing (Deployed)
Test Outbound (
/send): Usecurlor Postman to send a POST request to your deployed API Gateway/sendendpoint:curl -X POST https://<your-api-id>.execute-api.<region>.amazonaws.com/send \ -H ""Content-Type: application/json"" \ -d '{ ""to"": ""+1RECIPIENTNUMBER"", ""message"": ""Hello from deployed Fastify Lambda!"" }'Replace
<your-api-id>and<region>with your API Gateway details, and+1RECIPIENTNUMBERwith a test number. Verify the message is received. Check CloudWatch Logs for your Lambda function for details.Test Inbound (
/webhook/snsand Auto-Reply):fastify-sms-handlerLambda function. You should see logs indicating:/webhook/sns.console.logshowing the received message details.Test Health Check (
/health):You should receive a JSON response like:
{""status"":""ok"",""timestamp"":""...""}.Conclusion and Next Steps
You have successfully built and deployed a serverless two-way SMS application using Fastify, Node.js, and several AWS services. This provides a scalable foundation for handling SMS communication.
Potential Enhancements:
sns-validatorto prevent SSRF and ensure messages genuinely originate from your SNS topic.