Use CloudFormation to specify SMS (short message) as the destination for SNS notifications

Use CloudFormation to specify SMS (short message) as the destination for SNS notifications

As indicated on the following pages, there are a number of destinations available for notifications of SNS events.

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

This page confirms how to notify SMS (Short Message Service).

Environment

Diagram of specifying SMS (short message) as the destination for SNS notifications.

Create two resources.

The first is SNS topics.
Select SMS as the notification destination and specify a cell phone number.

The second is a Lambda function.
This function acts as a publisher that sends messages to SNS topics.
The runtime environment for the function is Python 3.12.

CloudFormation template files

The above configuration is built with CloudFormation.
The CloudFormation template is placed at the following URL

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.

Explanation of key points of template files

SNS Topics

Resources:
  Topic:
    Type: AWS::SNS::Topic
    Properties:
      FifoTopic: false
      Subscription: 
        - Endpoint: !Ref PhoneNumber
          Protocol: sms
      TopicName: !Sub "${Prefix}-sns-topic"
Code language: YAML (yaml)

The Subscription property is the key point.
Specify the cell phone number to be notified in the Endpoint property.
Specify “sms” for Protocol property.

(Reference) Lambda function

The role of this function is to send a message to the SNS.
The following settings are used to notify messages.

  • Title: hogehoge
  • Body: fugafuga

For the specific code to be executed by the function, please refer to the following page.

あわせて読みたい
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...

Architecting

Use CloudFormation to build this environment and check its actual behavior.

Create a CloudFormation stacks and check the resources in the stacks

Create CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following pages.

あわせて読みたい
CloudFormation’s nested stack 【How to build an environment with a nested CloudFormation stack】 Examine nested stacks in CloudFormation. CloudFormation allows you to nest stacks. Nested ...

Check the SNS.

Detail of SNS 01.

The SNS topic has been successfully created.
When I check the device subscribing to this topic, it does indeed specify “SMS” as the protocol and a phone number as the endpoint.

Check the Lambda function.

Detail of Lambda 01.

The Lambda function is successfully created.

(Reference) If your AWS account is in the SMS sandbox, register the phone number to be notified in advance.

Before checking the operation, care must be taken.

By default, your AWS account is in the SMS Sandbox.
The AWS official description of the SMS sandbox is as follows

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, if your AWS account is in the SMS sandbox, it is important to

You can send SMS messages only to verified destination phone numbers.

SMS sandbox

Before operating, register the phone number to be notified.

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.

Operation Check

Now that we are ready, we execute the Lambda function.

Detail of Lambda 02.

function has been successfully executed.
The message has now been sent to the SNS topic.

Check the SMS application on your phone again.

Detail of SNS 07.

Sure enough, a message (fugafuga) was sent.
The string (hogehoge) specified as the subject seems to be ignored.

Summary

We have introduced how to designate SMS as the notification destination for SNS.