How to Send Notifications via SMS Using AWS SNS

TOC

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:

あわせて読みたい
Amazon SNS event destinations - Amazon Simple Notification Service Learn about Amazon SNS event destinations Learn how Amazon SNS delivers events to various destinations, categorized into application-to-application (A2A) messag...

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

Diagram of specifying SMS (short message) as the destination for SNS notifications.
  • 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

Detail of SNS 01.

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.

Detail of SNS 02.

Click on “Add phone number”.

Detail of SNS 03.

After entering the phone number to be registered, press “Add phone number”.

Detail of SNS 04.

A one-time password for authentication was sent to the SMS application on the designated cell phone.

Enter this on the registration page.

Detail of SNS 05.

After entering the one-time password, press “Verify phone number”.

Detail of SNS 06.

A phone number has been registered. You can now notify this number of your message.

Lambda Function

Detail of Lambda 01.

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:

あわせて読みたい
Introduction to SNS with CFN – email version 【Introduction to SNS with CFN - email version】 AWS SNS is a messaging service. In this introductory article, we will show you how to specify Email as the n...

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

GitHub
awstut-fa/159 at main · awstut-an-r/awstut-fa Contribute to awstut-an-r/awstut-fa development by creating an account on GitHub.

Verification

Execute the Lambda function.

Detail of Lambda 02.

The function executed successfully, and a message was sent to the SNS topic.

Check the SMS app on your mobile phone.

Detail of SNS 07.

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.

TOC