Developer Quick-Start: A comprehensive guide to building a serverless two-way SMS system using Fastify, Node.js, AWS Pinpoint, SNS, Lambda, and API Gateway. Want to skip 10DLC vetting queues, carrier surcharges, and multi-step SDK boilerplate? You can send the same SMS directly via SMSRoute's REST API with instant no-KYC provisioning.
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:
- Send outbound SMS messages programmatically via AWS Pinpoint.
- Receive inbound SMS messages sent to your Pinpoint phone number.
- Process inbound messages using a Fastify application running on AWS Lambda.
- Implement basic auto-reply functionality.
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:
- Node.js: Runtime environment for our backend application.
- Fastify: A fast and low-overhead web framework for Node.js, suitable for building APIs and serverless functions.
- AWS Pinpoint: Used for sending outbound SMS messages and providing the dedicated phone number.
- AWS SNS (Simple Notification Service): Used to receive notifications when an SMS is sent to our Pinpoint number.
- AWS Lambda: Serverless compute service to run our Fastify application code without managing servers.
- AWS API Gateway (HTTP API): Creates an HTTP endpoint that triggers our Lambda function, enabling SNS to send inbound message data to our application.
- AWS SDK for JavaScript (v2): Used within our Node.js application to interact with Pinpoint and SNS APIs. (See note in Section 1.3 regarding v3).
- dotenv: Module to manage environment variables securely.
- @fastify/aws-lambda: Fastify plugin to adapt our application for AWS Lambda execution.
System Architecture:
+-------------+ +-----------------+ +-----------+ +-----------------+ +-------------+
| API Client | ----> | API Gateway | ----> | AWS Lambda| ----> | AWS Pinpoint API| ----> | User's Phone| (Outbound)
| (e.g. curl)| | (POST /send) | | (Fastify) | | (sendMessages) | +-------------+
+-------------+ +-----------------+ +-----------+ +-----------------+
^ |
| | (Process & Auto-Reply)
| v
+-------------+ +-----------------+ +-----+-----+ +-----------------+ +-----------------+
| User's Phone| ----> | AWS Pinpoint | ----> | AWS SNS | ----> | API Gateway | ----> | AWS Lambda | (Inbound)
| (Sends SMS) | | (Receives SMS) | | (Topic) | | (POST /webhook) | | (Fastify App) |
+-------------+ +-----------------+ +-----------+ +-----------------+ +-----------------+
Prerequisites:
- An AWS account with permissions to manage IAM, Pinpoint, SNS, Lambda, and API Gateway.
- Node.js (v18 or later recommended) and npm installed locally.
- AWS CLI installed and configured locally (for deployment steps, optional but recommended).
- Basic understanding of Node.js, APIs, and AWS concepts.
- A text editor or IDE (like VS Code).
- A tool for testing API endpoints (like curl or 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-way
- Initialize Node.js Project:
npm init -y
- Install Dependencies:
npm install fastify aws-sdk dotenv @fastify/aws-lambda
- 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 .env file 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:
fastify-sms-two-way/
βββ .env.example # Example environment variables
βββ .gitignore # Git ignore file
βββ node_modules/ # Installed dependencies (managed by npm)
βββ package.json
βββ package-lock.json
βββ src/
β βββ app.js # Fastify application setup and routes
β βββ lambda.js # AWS Lambda handler entry point
β βββ utils/
β βββ aws.js # AWS SDK client initialization
βββ README.md # Project description (optional)
Create the src and src/utils directories.
- Create
.gitignore: Create a.gitignorefile in the project root to prevent committing sensitive files and dependencies:
# Environment variables
.env
# Node dependencies
node_modules/
# Development dependencies (if separate)
node_modules_dev/
# Build artifacts (if any)
dist/
build/
deployment_package.zip
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# OS generated files
.DS_Store
Thumbs.db
- 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.
.env
# AWS Credentials & Region (Primarily for LOCAL development)
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
AWS_REGION=us-east-1 # e.g., us-east-1, eu-west-1
# AWS Pinpoint Configuration
PINPOINT_APP_ID=YOUR_PINPOINT_APPLICATION_ID
PINPOINT_ORIGINATION_NUMBER=+1XXXXXXXXXX # Your purchased Pinpoint phone number in E.164 format
# AWS SNS Configuration (For inbound message handling)
SNS_TOPIC_ARN=arn:aws:sns:YOUR_REGION:ACCOUNT_ID:YOUR_TOPIC_NAME # ARN of the SNS topic
Security Note: The .env file 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.
- Go to the AWS IAM Console.
- Create a new User. Give it a descriptive name (e.g., fastify-sms-dev-user).
- Select ""Access key - Programmatic access"" as the credential type.
- Attach Policies (Least Privilege): Instead of using broad FullAccess policies, 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)
- Potentially iam:PassRole if your local setup involves assuming roles.
- Important: Securely store the generated Access key ID and Secret access key. These will go into your local .env file.
- 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)
- Potentially sns:Publish if the Lambda needs to publish to other topics.
- Note: The Lambda role does not typically need SNS Subscription permissions; the subscription is made to the Lambda's trigger (API Gateway).
- Set up AWS Pinpoint:
- Go to the AWS Pinpoint Console.
- If you don't have one, create a new Pinpoint project. Note the Project ID (also called Application ID).
- Navigate to ""SMS and voice"" settings within your project.
- Enable the SMS channel for the project if it's not already enabled.
- Go to ""Phone numbers"" under SMS and voice settings.
- Request a phone number. Choose a country and ensure the number supports SMS and Two-way SMS. This might require registration depending on the country (e.g., 10DLC for the US). Note the purchased Phone number in E.164 format (e.g., +12065550100).
- Once you have the number, select it. In the configuration panel at the bottom, find the ""Two-way SMS"" section.
- Enable Two-way SMS.
- For ""Incoming message destination,"" select ""Choose an existing SNS topic"". We will create the SNS topic next and come back here to select it.
- Create an SNS Topic:
- Go to the AWS SNS Console.
- Click ""Topics"" -> ""Create topic"".
- Choose ""Standard"" type.
- Give it a descriptive name (e.g., twoWaySMSHandler).
- Leave other settings as default and click ""Create topic"".
- Note the Topic ARN (Amazon Resource Name). It looks like arn:aws:sns:us-east-1:123456789012:twoWaySMSHandler.
- Link Pinpoint to SNS Topic:
- Go back to the AWS Pinpoint Console -> Phone numbers.
- Select your phone number again.
- In the ""Two-way SMS"" configuration, now select the SNS topic you just created (twoWaySMSHandler) from the dropdown.
- Click Save changes. Now, any SMS message sent to your Pinpoint number will be published to this SNS topic.
- 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.
'use strict';
const AWS = require('aws-sdk');
if (!process.env.AWS_LAMBDA_FUNCTION_NAME) {
require('dotenv').config();
console.log('Loaded .env file for local development.');
}
const requiredConfig = {
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'
};
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."");
}
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);
const pinpoint = new AWS.Pinpoint();
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,
};
- Uses dotenv only locally.
- Validates required environment variables.
- Configures SDK: Uses keys from .env locally, 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.
'use strict';
const Fastify = require('fastify');
const { pinpoint, pinpointAppId, pinpointOriginationNumber } = require('./utils/aws');
async function sendSms(destinationNumber, message, messageType = 'TRANSACTIONAL') {
if (!destinationNumber || !message) {
throw new Error('Destination number and message are required.');
}
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,
OriginationNumber: pinpointOriginationNumber,
}
}
}
};
try {
const result = await pinpoint.sendMessages(params).promise();
const messageResult = result.MessageResponse.Result[destinationNumber];
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}`);
}
}
function build(opts = {}) {
const app = Fastify(opts);
app.post('/send', {
schema: {
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'}
}
},
}
}
}, async (request, reply) => {
const { to, message } = request.body;
request.log.info(`Received request to send SMS | To: ${to}`);
try {
const result = await sendSms(to, message);
reply.code(200).send({
status: result.StatusMessage,
deliveryStatus: result.DeliveryStatus,
messageId: result.MessageId,
destination: to
});
} catch (error) {
request.log.error({ err: error }, `Failed to send SMS to ${to}`);
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.' });
}
}
});
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 {
if (messageType === 'SubscriptionConfirmation') {
request.log.info(`Received SNS Subscription Confirmation request. URL: ${request.body.SubscribeURL}`);
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;
}
if (messageType === 'Notification') {
request.log.info('Processing SNS Notification...');
const snsMessage = request.body;
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;
const messageBody = inboundSmsData.messageBody;
console.log(`Received SMS | From: ${senderNumber} | To: ${recipientNumber} | Message: ""${messageBody}""`);
const replyMessage = `Thanks for your message! We received: ""${messageBody}"". We'll get back to you soon.`;
try {
await sendSms(senderNumber, replyMessage);
console.log(`Auto-reply sent to ${senderNumber}`);
} catch (replyError) {
request.log.error({ err: replyError }, `Failed to send auto-reply to ${senderNumber}`);
}
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');
reply.code(500).send({ error: 'Internal Server Error', message: 'Failed to process incoming message.' });
}
});
app.get('/health', async (request, reply) => {
return { status: 'ok', timestamp: new Date().toISOString() };
});
return app;
}
module.exports = { build, sendSms };
- Includes stronger warnings about SNS subscription confirmation and SSRF.
- Includes basic E.164 validation for to number in /send schema.
- Adds logging and error handling for SNS message parsing.
- Improves error handling in /send route based on error type.
- Create Lambda Handler (
src/lambda.js): This file uses@fastify/aws-lambdato wrap our Fastify application instance.
'use strict';
const awsLambdaFastify = require('@fastify/aws-lambda');
const { build } = require('./app');
const app = build({
logger: {
level: process.env.LOG_LEVEL || 'info',
}
});
const proxy = awsLambdaFastify(app);
exports.handler = async (event, context) => {
console.log(`Lambda handler invoked. Request ID: ${context.awsRequestId}, Event source: ${event.requestContext?.domainName || 'Unknown'}`);
try {
const result = await proxy(event, context);
return result;
} catch (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);
}
return {
statusCode: 500,
body: JSON.stringify({ error: 'Internal Lambda Error', message: error.message || 'An unexpected error occurred.' }),
headers: { 'Content-Type': 'application/json' },
};
}
};
- Initializes Fastify with logging enabled.
- Uses @fastify/aws-lambda proxy.
- Exports the standard Lambda handler, including basic logging and error handling.
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:
""scripts"": {
""start:local"": ""node -r dotenv/config ./scripts/run-local.js"",
""test"": ""echo \""Error: no test specified\"" && exit 1""
},
- -r dotenv/config: Preloads dotenv to load variables from .env.
- Install
pino-prettyand Create Local Runner (scripts/run-local.js): First, install the development dependency for nice local logging:
npm install --save-dev pino-pretty
Now, create a scripts directory and add run-local.js. This script runs the Fastify
app directly using its built-in server.
'use strict';
const { build } = require('../src/app');
const PORT = process.env.PORT || 3000;
const HOST = '127.0.0.1';
const start = async () => {
const app = build({
logger: {
level: 'info',
transport: {
target: 'pino-pretty',
options: {
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname',
colorize: true,
},
},
}
});
try {
await app.listen({ port: PORT, host: HOST });
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) {
console.error('Error starting local server:', err);
process.exit(1);
}
};
start();
- Uses pino-pretty for improved local log readability.
- Uses app.listen for standard HTTP server behavior.
- Run Locally: Make sure your
.envfile is populated with your IAM User credentials and other config.
npm run start:local
The 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 +1RECIPIENTNUMBER with a valid test phone number. Check your terminal logs (nicely
formatted by pino-pretty) and the recipient's phone for the message. Note on testing
inbound: Testing the /webhook/sns route locally requires simulating an SNS notification.
This is complex because AWS SNS needs a publicly accessible HTTPS endpoint to send notifications to. Tools
like ngrok can 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.
- First, ensure development dependencies are pruned:
npm prune --omit=dev
- Create the zip file from the project root directory:
zip -r deployment_package.zip src node_modules package.json package-lock.json
This command packages the src directory (containing app.js, lambda.js,
utils/aws.js), the production node_modules, and package files.
- Create the Lambda Function:
- Go to the AWS Lambda Console.
- Click ""Create function"".
- Select ""Author from scratch"".
- Function name: fastify-sms-handler (or similar).
- Runtime: Select a recent Node.js version (e.g., Node.js 18.x or 20.x).
- Architecture: x86\_64 or arm64.
- Permissions: This is critical.
- Choose ""Create a new role with basic Lambda permissions"" OR select an existing role.
- Edit the role: Go to the IAM console and find the role created/selected for the Lambda function (e.g., fastify-sms-handler-role-xxxx).
- Attach policies (or create an inline policy) granting the necessary permissions:
- pinpoint:SendMessages (to send replies from the webhook).
- The basic execution role already includes CloudWatch Logs permissions (logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents).
- Do not add SNS subscription permissions here.
- Click ""Create function"".
- Upload Code:
- In the Lambda function's configuration page, find the ""Code source"" section.
- Click ""Upload from"" -> "".zip file"".
- Upload the deployment\_package.zip file you created.
- Click ""Save"".
- Configure Handler and Environment Variables:
- In the ""Runtime settings"" section, click ""Edit"".
- Set the Handler to src/lambda.handler (pointing to exports.handler in src/lambda.js).
- Click ""Save"".
- Go to the ""Configuration"" tab -> ""Environment variables"".
- Click ""Edit"".
- Add the following environment variables (these are used by src/utils/aws.js when 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 to debug, warn, error to control logging verbosity (defaults to info).
- Important: Do not add AWS\_ACCESS\_KEY\_ID or AWS\_SECRET\_ACCESS\_KEY here. The Lambda function will use the permissions granted by its Execution Role.
- Click ""Save"".
- Create API Gateway (HTTP API): We'll use an HTTP API for simplicity and cost-effectiveness.
- Go to the AWS API Gateway Console.
- Click ""Build"" under HTTP API.
- Click ""Add integration"".
- Integration type: Lambda.
- AWS Region: Select the region where your Lambda function is.
- Lambda function: Choose your fastify-sms-handler function.
- API name: SMSTwoWayAPI (or similar).
- Click ""Next"".
- Configure routes:
- Method: POST, Resource path: /send. Integration target: fastify-sms-handler.
- Method: POST, Resource path: /webhook/sns. Integration target: fastify-sms-handler.
- Method: GET, Resource path: /health. Integration target: fastify-sms-handler.
- Click ""Next"".
- Configure stages: Default stage $default is fine for now. Ensure ""Auto-deploy"" is enabled.
- Click ""Next"".
- Review and click ""Create"".
- Note the Invoke URL provided after creation (e.g., https://abcdef123.execute-api.us-east-1.amazonaws.com).
- Subscribe SNS Topic to API Gateway Endpoint:
- Go back to the AWS SNS Console.
- Select your twoWaySMSHandler topic.
- Go to the ""Subscriptions"" tab.
- Click ""Create subscription"".
- Protocol: HTTPS.
- Endpoint: Enter the API Gateway Invoke URL specifically for the webhook route. It will be like: https://abcdef123.execute-api.us-east-1.amazonaws.com/webhook/sns.
- Enable raw message delivery: Check this box. This sends the raw Pinpoint JSON message directly to your endpoint, which our code expects (JSON.parse(snsMessage.Message)). If unchecked, SNS wraps the message in its own JSON structure.
- Click ""Create subscription"".
- Confirmation: The subscription status will be ""Pending confirmation"". Check your Lambda function's CloudWatch logs (associated with the /webhook/sns route). You should see the log message containing the SubscribeURL from 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
+1RECIPIENTNUMBER with a test number. Verify the message is received. Check CloudWatch Logs for
your Lambda function for details.
- Test Inbound (
/webhook/snsand Auto-Reply):
- Send an SMS message from your test phone to your AWS Pinpoint phone number.
- Check CloudWatch Logs: Monitor the logs for your fastify-sms-handler Lambda function. You should see logs indicating:
- Receipt of the SNS notification on /webhook/sns.
- Parsing of the inbound SMS data.
- The console.log showing the received message details.
- Logs related to sending the auto-reply via Pinpoint.
- Check Your Test Phone: You should receive the auto-reply message (""Thanks for your message!..."").
- Test Health Check (
/health):
curl https://<your-api-id>.execute-api.<region>.amazonaws.com/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 Message Signature Validation: Implement robust validation for incoming SNS messages using libraries like sns-validator to prevent SSRF and ensure messages genuinely originate from your SNS topic.
- More Sophisticated Routing/Logic: Instead of a simple auto-reply, route messages based on keywords, store conversation history in a database (like DynamoDB), or integrate with other business systems.
- Error Handling and Retries: Implement more robust error handling, potentially using Dead Letter Queues (DLQs) for Lambda or SNS if processing fails.
- Rate Limiting: Add rate limiting to your API Gateway endpoints to prevent abuse.
- Monitoring and Alerting: Set up CloudWatch Alarms based on Lambda errors, invocation counts, or specific log messages.
- AWS SDK v3: Migrate the code to use the modular AWS SDK v3 for JavaScript.
- Infrastructure as Code (IaC): Define your AWS resources (Lambda, API Gateway, SNS, IAM Roles) using tools like AWS SAM, AWS CDK, Serverless Framework, or Terraform for repeatable deployments.
- Cost Optimization: Monitor Pinpoint, Lambda, and API Gateway costs. Choose appropriate message types (Transactional vs. Promotional).
Simpler Alternative: Send the Same SMS With SMSRoute (No-KYC, Direct Routes)
If you want to replace legacy aggregator SDK boilerplate, multi-week compliance queues, and opaque carrier surcharges with a single HTTP request, SMSRoute delivers global SMS over direct Tier-1 carrier interconnects with instant API access and transparent per-country pricing.
curl -X POST https://api.sendsmsnokyc.com/messages \
-H "x-api-key: $SMSROUTE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": ["+14155551234"],
"channel": ["sms"],
"body": "Your verification code is 482910"
}'
- Instant onboarding: No KYC paperwork or sales queues β grab an API key and fund with crypto (BTC/USDT) or card.
- Direct Tier-1 routes: Verify real handset delivery, DLR latency, and Sender ID preservation before launch via SMSRoute Live Carrier Testing.
- Transparent global pricing: Compare exact per-country termination rates on our live pricing page.
Ship Global SMS in One API Call
Skip legacy SDK sprawl and carrier bottlenecks. Send OTPs, alerts, and bulk campaigns across 190+ countries with SMSRoute.
Explore SMSRoute Developers β