- Newest
- Most votes
- Most comments
Greeting
Hi Frank,
Thank you for sharing your detailed question and code. Let’s address the SNS delivery issue step by step to get it resolved.
Clarifying the Issue
The primary issue is that SMS messages, while logged as "Message sent successfully" by the Lambda function, are not being delivered. Delivery statistics indicate a 0% success rate after a UTC day change, even though the phone number is verified in the Sandbox and not blocked by the carrier. Spending limits and account configurations seem appropriate, as SMS activity shows a small spend.
Potential causes include sandbox limitations, SMS type mismatches, or regional restrictions. We’ll walk through each potential point of failure and suggest a solution.
Key Terms
- AWS SNS (Simple Notification Service): A service to send notifications, including SMS, to multiple destinations.
- Sandbox Mode: A restricted environment for testing SNS messages, requiring explicit verification of phone numbers.
- E.164 Format: The standardized format for international phone numbers required by SNS.
- CloudWatch Logs: A tool to analyze execution details and debug Lambda functions.
- Transactional SMS: A message type optimized for critical use cases like authentication, with higher delivery rates.
The Solution (Our Recipe)
Steps at a Glance:
- Verify Sandbox settings and upgrade to Production.
- Confirm spending limits, SMS type, and regional support.
- Analyze SNS Delivery Logs and CloudWatch Logs for failures.
- Enhance the Lambda function with improved error handling and validation.
Step-by-Step Guide:
Step 1: Verify Sandbox Settings and Upgrade to Production
- Navigate to the Text Messaging Preferences in the SNS console and confirm that the destination phone number is verified in Sandbox Mode. Sandbox limits may block unrestricted delivery.
- To move beyond Sandbox mode, follow these steps to upgrade to Production:
- Open the SNS console and go to Text Messaging Preferences.
- Enable Production SMS.
- Update configurations to support non-verified phone numbers.
Step 2: Confirm Spending Limits, SMS Type, and Regional Support
- Check the Spending Limits in the SNS dashboard under SMS Preferences. By default, the limit is $1.00, which may restrict further messages. Raise the limit to allow more usage.
- Set the SMS message type to Transactional. AWS prioritizes transactional messages over promotional ones, which may be deprioritized by carriers.
- Review the Regional SMS Support in AWS’s documentation. Ensure that both your AWS region and the destination region support SMS delivery. Use this link to verify supported countries and regions.
Step 3: Analyze SNS Delivery Logs and CloudWatch Logs
- In the SNS console, enable Delivery Status Logging for SMS. This will provide detailed carrier responses and error codes for each sent message. Use this guide to configure delivery logs.
- Check CloudWatch Logs for the Lambda function to locate the MessageId returned by the SNS
sendoperation. - Cross-reference the MessageId in the Delivery Logs for insights. Example of a failed delivery log:
From this, you can identify issues like invalid phone numbers or carrier blocks.{ "deliveryStatus": "FAILURE", "statusCode": "InvalidPhoneNumber", "providerResponse": "Phone number is not reachable" }
Step 4: Enhance the Lambda Function with Improved Error Handling
Original Code with Inline Comments:
try { // Send the SMS message using the SNS client const response = await snsClient.send(command); // Log the success message along with the returned MessageId console.log("Message sent successfully:", response.MessageId); // Return the response object directly (not structured for readability) return response; } catch (error) { // Log the error if the message fails to send console.error("Error sending message:", error); }
This code attempts to send an SMS message and logs any errors encountered. However, it lacks validation for input parameters (like phone numbers) and doesn't provide structured error feedback to the caller.
Enhanced Code with Inline Comments:
try { // Prepare the parameters for the PublishCommand const params = { PhoneNumber: "+<E.164_PHONE_NUMBER>", // Replace with a valid E.164 formatted number Message: code, // The message content, such as an OTP or notification }; // Validate the phone number format using a regular expression for E.164 if (!/^\+\d{1,15}$/.test(phone)) { // Throw an error if the phone number doesn't match the required format throw new Error("Invalid phone number format. Must be in E.164 format."); } // Create a new PublishCommand with the parameters const command = new PublishCommand(params); // Send the SMS message using the SNS client const response = await snsClient.send(command); // Log the success message along with the returned MessageId console.log("Message sent successfully:", response.MessageId); // Return a structured response indicating success return { statusCode: 200, body: JSON.stringify({ success: true, messageId: response.MessageId }), }; } catch (error) { // Log the error message for debugging purposes console.error("Error sending message:", error.message); // Return a structured response indicating failure, including error details return { statusCode: 500, body: JSON.stringify({ success: false, // Indicate failure errorType: error.name, // Provide the error type (e.g., ValidationError) errorMessage: error.message, // Provide the error message }), }; }
This enhanced version:
- Validates the phone number format before attempting to send the message, reducing errors from invalid inputs.
- Provides detailed error messages, including error type and description, in a structured response.
- Includes inline comments explaining every key line of code, ensuring clarity for readers.
Closing Thoughts
Frank, by moving out of Sandbox mode, validating your configurations, and analyzing logs, you should be able to resolve the delivery issue. The enhanced Lambda function will make future troubleshooting more efficient. If the problem persists, contact AWS Support with specific Message IDs and delivery logs for deeper investigation.
Let me know if you need further clarification or assistance. 😊
Farewell
Best of luck, and I hope this resolves your SNS delivery challenges! 🚀
Cheers, Aaron 😊
answered 2 years ago
Relevant content
asked 3 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago

To which destination country you are sending messages ? Normal messages to the destination number is receiving ?
destination country is ca nada (+1) for dev now. no sms is received now
was using us-east-2 at the beginning, work again as region changed to ca-central-1 as I am in can ada now