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.
Environment
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
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.
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.
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.
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.
Check the AWS IoT rules created.
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.
You can check the SNS topics, etc. to which you are notified.
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.
Then issue a message for the topic device/32/data.
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.
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.
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.