How to Send SMS Using AWS SNS: Simple Guide
To send an SMS using
AWS SNS, create an SNS client and call the publish method with the recipient's phone number and message text. The phone number must be in E.164 format (e.g., +1234567890).Syntax
The basic syntax to send an SMS with AWS SNS involves calling the publish method on the SNS client. You provide the PhoneNumber in E.164 format and the Message text you want to send.
PhoneNumber: The recipient's phone number with country code, e.g.,+14155552671.Message: The text content of the SMS.MessageAttributes(optional): Settings like SMS type (Promotional or Transactional) and sender ID.
javascript
sns.publish({
PhoneNumber: '+1234567890',
Message: 'Your message here',
MessageAttributes: {
'AWS.SNS.SMS.SMSType': {
DataType: 'String',
StringValue: 'Transactional'
},
'AWS.SNS.SMS.SenderID': {
DataType: 'String',
StringValue: 'MySender'
}
}
});Example
This example shows how to send an SMS using AWS SDK for JavaScript (v3). It creates an SNS client, then sends a transactional SMS message to a phone number.
javascript
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns"; const snsClient = new SNSClient({ region: "us-east-1" }); async function sendSms() { try { const params = { PhoneNumber: "+14155552671", Message: "Hello! This is a test SMS from AWS SNS.", MessageAttributes: { "AWS.SNS.SMS.SMSType": { DataType: "String", StringValue: "Transactional" } } }; const command = new PublishCommand(params); const data = await snsClient.send(command); console.log("Message sent, ID:", data.MessageId); } catch (err) { console.error("Error sending SMS:", err); } } sendSms();
Output
Message sent, ID: <unique-message-id>
Common Pitfalls
- Incorrect phone number format: Always use E.164 format starting with '+'.
- Missing permissions: Ensure your IAM role/user has
sns:Publishpermission. - SMS type not set: Without setting
SMSType, messages default toPromotional, which may be blocked in some regions. - Region mismatch: Use the correct AWS region where SNS SMS is supported.
javascript
/* Wrong: Missing '+' in phone number */ sns.publish({ PhoneNumber: '14155552671', // Incorrect format Message: 'Test message' }); /* Right: Correct E.164 format */ sns.publish({ PhoneNumber: '+14155552671', Message: 'Test message' });
Quick Reference
| Parameter | Description | Example |
|---|---|---|
| PhoneNumber | Recipient phone number in E.164 format | +14155552671 |
| Message | Text content of the SMS | Hello from AWS SNS! |
| AWS.SNS.SMS.SMSType | SMS type: Transactional or Promotional | Transactional |
| AWS.SNS.SMS.SenderID | Custom sender ID (max 11 chars) | MyCompany |
Key Takeaways
Use E.164 format with '+' for phone numbers when sending SMS via SNS.
Set SMS type to 'Transactional' or 'Promotional' using MessageAttributes for proper delivery.
Ensure your AWS credentials have sns:Publish permission.
Use the AWS SDK's publish method with PhoneNumber and Message parameters.
Check AWS region supports SNS SMS before sending messages.