How to Send Notifications via SMS Using AWS SNS
In AWS SNS (Simple Notification Service), you can choose from a variety of notification destinations. According to the official documentation, numerous SNS event destinations are available:
In this article, we will explore how to send notifications via SMS (Short Message Service). We will create an SNS topic, register a mobile phone number, and set up SMS notifications. Additionally, we’ll use a Lambda function to send messages to the SNS topic. This setup allows you to receive notifications via SMS using AWS SNS.
Configuration
- Create an SNS topic and specify SMS as the notification destination.
- Create a Lambda function that sends messages to the SNS topic.
Resources
SNS Topic
We subscribe to the SNS topic by specifying the mobile phone number and the SMS protocol.
Pre-registering the Destination Phone Number When AWS Account is in SMS Sandbox
Before proceeding, note that by default, your AWS account is in the SMS sandbox. According to AWS’s official documentation:
When you start using Amazon SNS to send SMS messages, your AWS account is in the SMS sandbox. The SMS sandbox provides a safe environment for you to try Amazon SNS features without risking your reputation as an SMS sender.
SMS sandbox
Also, when your AWS account is in the SMS sandbox:
You can send SMS messages only to verified destination phone numbers.
SMS sandbox
Therefore, you need to register the destination phone number before testing.
Click on “Add phone number”.
After entering the phone number to be registered, press “Add phone number”.
A one-time password for authentication was sent to the SMS application on the designated cell phone.
Enter this on the registration page.
After entering the one-time password, press “Verify phone number”.
A phone number has been registered. You can now notify this number of your message.
Lambda Function
This function is responsible for sending messages to SNS. It sends messages with the following settings:
- Subject: hogehoge
- Body: fugafuga
For the specific code executed in the function, please refer to:
CloudFormation Template
SNS Topic
Resources:
Topic:
Type: AWS::SNS::Topic
Properties:
FifoTopic: false
Subscription:
- Endpoint: !Ref PhoneNumber
Protocol: sms
TopicName: !Sub "${Prefix}-sns-topic"
Code language: YAML (yaml)
Full Template
Verification
Execute the Lambda function.
The function executed successfully, and a message was sent to the SNS topic.
Check the SMS app on your mobile phone.
The message “fugafuga” was delivered. It appears that the specified subject “hogehoge” is ignored.
Conclusion
We have demonstrated how to specify SMS as the notification destination in AWS SNS. By creating an SNS topic and sending messages from a Lambda function, we enabled SMS notifications. This setup allows you to receive important information directly on your mobile phone.