Guide: Implementing RedwoodJS AWS SNS Inbound/Two-Way Messaging - code-examples -

Frequently Asked Questions

You can receive SMS messages in your RedwoodJS application by integrating with Amazon Pinpoint and AWS SNS. Pinpoint provides the phone number, and SNS forwards incoming messages to your RedwoodJS API function as HTTPS POST requests. This allows two-way SMS communication within your application.
Amazon Pinpoint is used to acquire and manage the phone number that can send and receive SMS messages. It integrates with SNS to forward incoming messages to your RedwoodJS application, enabling two-way SMS communication.
AWS SNS is used with Amazon Pinpoint because Pinpoint publishes incoming SMS messages to an SNS topic. The SNS topic then acts as a distribution hub, securely forwarding the message to your RedwoodJS application via an HTTPS POST request to your designated API function.
You should *disable* Raw Message Delivery in your SNS subscription settings. When disabled, you receive the standard SNS message wrapper, which includes important metadata and security information needed for validation with the `sns-validator` library. Enabling it sends only the Pinpoint payload, breaking compatibility with standard validation methods.
Yes, you can store received SMS messages in a database. The provided code example demonstrates how to use Prisma, RedwoodJS's ORM, to define a database schema, create a migration, and store message details like body, sender/receiver numbers, and timestamps in a database like PostgreSQL or SQLite.
Two-way SMS is enabled within the Amazon Pinpoint console. After acquiring a phone number, navigate to its settings, go to the Two-Way SMS tab, enable two-way messaging, and select a new or existing SNS topic as the message destination. This configures Pinpoint to forward incoming messages to SNS, which then forwards to your application.
RedwoodJS API functions, including the SMS webhook handler, typically deploy as AWS Lambda functions when using the Serverless Framework. Lambda executes the function code in a serverless environment whenever a message is received from SNS, triggering the message processing logic.
Use the `sns-validator` library. It verifies the digital signature attached to incoming SNS messages, ensuring they originated from AWS and haven't been tampered with. The code provides an example of how to validate within your RedwoodJS function handler before processing the message content.
When you first subscribe your RedwoodJS endpoint to the SNS topic, SNS sends a 'SubscriptionConfirmation' message. Your function must handle this by extracting the 'SubscribeURL' from the message and making an HTTPS GET request to that URL. This confirms to SNS that your endpoint is valid and ready to receive notifications.
If messages aren't arriving, check your Pinpoint and SNS configurations in the AWS Console. Ensure the Pinpoint number is properly configured for two-way SMS, and that the SNS topic exists. Double-check that the subscription status is 'Confirmed', the endpoint URL is absolutely correct, and 'Raw Message Delivery' is *disabled*. Verify the subscription is connected to the intended RedwoodJS endpoint and look for any errors by checking your function's CloudWatch logs.
SNS signature validation failures are often due to incorrect handling of the incoming JSON, attempting to validate only the Pinpoint payload, or having accidentally enabled raw message delivery on the SNS subscription. Ensure you're passing the full SNS message object to the `sns-validator`, the message hasn't been modified, and raw message delivery is disabled. In rare cases, clock skew on the server running your function could also be a factor.
A pending SNS subscription confirmation usually means your RedwoodJS function failed to process the initial 'SubscriptionConfirmation' message from SNS. Check your function's CloudWatch logs for errors. Verify your function can reach the 'SubscribeURL', and that the function is correctly configured to handle this specific message type (by making the confirmation GET request).
Logs for your RedwoodJS SNS integration are primarily found in AWS CloudWatch. Look for the log group associated with your deployed Lambda function. API Gateway logs can also provide valuable information on the incoming request and the response your function returns.
Crucially, always validate the SNS message signature using a library like `sns-validator`. Never disable this. Always use HTTPS. Sanitize any user-provided data within the message body. Follow the principle of least privilege by granting your Lambda function only the necessary IAM permissions. Consider rate limiting at the API Gateway level if you anticipate high volumes or different exposure methods, and never commit AWS keys or database credentials to version control.