Notifying SNS using AWS IoT rules

Notifying SNS using AWS IoT rules

On this page, you will see how to use AWS IoT rules to notify SNS of the contents of MQTT messages.

The SNS (sns) action sends the data from an MQTT message as an Amazon Simple Notification Service (Amazon SNS) push notification.

SNS

In this article, we will check the following page to see how to notify SNS.

あわせて読みたい
Tutorial: Sending an Amazon SNS notification - AWS IoT Core This tutorial demonstrates how to create an AWS IoT rule that sends MQTT message data to an Amazon SNS topic so that it can be sent as an SMS text message.

Environment

Diagram of notifying SNS using AWS IoT rules

Create an AWS IoT rule.
This rule notifies SNS of messages issued to the topic.

In addition, create an IAM role for AWS IoT rules.

Select SMS as the notification destination for SNS topics and specify a cell phone number.

CloudFormation template files

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

GitHub
awstut-fa/160 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

AWS IoT Rules

Resources:
  TopicRule:
    Type: AWS::IoT::TopicRule
    Properties:
      RuleName: temp_limit_notify
      TopicRulePayload: 
        Actions: 
          - Sns: 
              MessageFormat: RAW
              RoleArn: !GetAtt TopicRuleRole.Arn
              TargetArn: !Ref SnsTopicArn
        AwsIotSqlVersion: 2016-03-23
        RuleDisabled: false
        Sql: !Sub |
          SELECT topic(2) as device_id, 
              temperature as reported_temperature, 
              30 as max_temperature 
            FROM '${TopicName}' 
            WHERE temperature > 30
Code language: YAML (yaml)

For basic information on AWS IoT rules, please refer to the following pages.

あわせて読みたい
Create AWS IoT rules and republish MQTT messages 【Create AWS IoT rules and republish MQTT messages】 AWS IoT has a feature called AWS IoT Rules. AWS IoT rules send data from your devices to other AWS servi...

To send a message to an SNS, set the Sns property.

The MessageFormat property allows you to set the format of the message to be sent.
You can select either “JSON” or “RAW” for this property, and the latter is specified in this case.

The TargetArn property specifies the SNS topic to be notified.

The Sql property defines the specific rule.
In this case, we will create a message consisting of three pieces of data.

The first is data named device_id.
This data uses the second string from the following topic function, which splits the topic name with a slash.

Returns the topic to which the message that triggered the rule was sent. If no parameter is specified, the entire topic is returned.

topic(Decimal)

The second is data named reported_temperature.
This uses the temperature data contained in the message delivered to the original topic.

The third data is named max_temperature.
This value is fixed at “30”.

The FROM clause specifies the name of the topic from which to retrieve data.
In this configuration, the topic device/+/data is targeted.
The topic name is specified using CloudFormation’s built-in function Sub.

However, the WHERE clause is used to set conditions on the data to be processed.
Specifically, the SNS will be notified only when the value of temperature is greater than 30.

Below are the IAM roles for AWS IoT rules.

Resources:
  TopicRuleRole:
    Type: AWS::IAM::Role
    DeletionPolicy: Delete
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Action: sts:AssumeRole
            Principal:
              Service:
                - iot.amazonaws.com
      Policies:
        - PolicyName: TopicRulePolicy
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - sns:Publish
                Resource:
                  - !Ref SnsTopicArn
Code language: YAML (yaml)

In the inline policy for the IAM role, give the SNS the necessary permissions for notification.
The required permissions are listed on the following page.

あわせて読みたい
SNS - AWS IoT Core Use the SNS rule action to send an MQTT message from AWS IoT as an Amazon Simple Notification Service push notification.

Specify this IAM role in the RoleArn property described above.

(Reference) 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)

Set SMS as the protocol and the cell phone number as the notification destination.

For information on how to notify messages to SMS, please see the following page.

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

In particular, by default, messages cannot be notified to unregistered phone numbers because the AWS account is placed in the SMS sandbox.
Please read the page above to learn more about this.

Architecting

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

Create 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 AWS IoT rules created.

Detail of AWS IoT 01.

You can see that the SQL expression that is the body of the rule has been successfully set.
We can also see that the rule targets messages issued to the topic device/+/data.

You can also see that notifications to SNS are specified as an action.

Check the details of the notification action.

Detail of AWS IoT 02.

You can check the SNS topics, etc. to which you are notified.

Detail of AWS 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.

Operation Check

Now that you are ready, access the MQTT test client page to check the operation.

First, subscribe to the topic device/+/data.

Detail of AWS IoT 03.

Then issue a message for the topic device/32/data.

Detail of AWS IoT 04.

The topic device/+/data allows us to receive the previous message.
This topic can receive the previous message by wildcard notation.

Looking at the temperature value, it is “38”, which is greater than 30.
This means that this message meets the requirements of the AWS IoT rule, so the message will be sent to the SNS topic.

Check the SMS application on your phone.

Detail of AWS IoT 05.

The message was indeed sent.

Looking at the content of the message, it contains three pieces of data: device_id, reported_temperature, and max_temperature.
Each is as specified in the SQL expression.

Send the message one last time.
Issue a message for the topic device/33/data.

Detail of AWS IoT 06.

The message was successfully issued.

However, no short message was received on the cell phone.
This is because the temperature value of the issued message was “28”, which is less than 30.
This means that the message was not sent to the SNS topic because it did not meet the requirements of the AWS IoT rule.

Summary

AWS IoT rules can be used to notify SNS of messages.
Appropriate use of the WHERE clause can be used to limit the target messages.