How to Republish MQTT Messages Using AWS IoT Rules

TOC

How to Republish MQTT Messages Using AWS IoT Rules

AWS IoT provides a feature called AWS IoT Rules.

AWS IoT rules send data from your devices to other AWS services. They listen for specific MQTT messages, format the data in the message payloads, and send the result to other AWS services.

Creating AWS IoT rules to route device data to other services

By using AWS IoT Rules, you can send data within messages to various AWS resources.

In this article, we will explore how to republish received MQTT messages. We will create AWS IoT Rules and an IAM role to republish messages from one topic to another. As a result, you can efficiently extract and process message content and redistribute it to different topics.

Refer to the following page to see how to re-issue received MQTT messages.

あわせて読みたい
Tutorial: Republishing an MQTT message - AWS IoT Core This tutorial demonstrates how to create an AWS IoT rule that publishes an MQTT message when a specified MQTT message is received. The incoming message payload ...

Configuration

Diagram of creating AWS IoT rules and republish MQTT messages.
  • Create an AWS IoT Rule that republishes messages published to a topic to another topic.
  • Create an IAM role for the AWS IoT Rule.

Resources

AWS IoT Rule

Detail of AWS IoT 1.

The rule is structured like an SQL statement. For details, please refer to the following page:

あわせて読みたい
AWS IoT SQL reference - AWS IoT Core See the following SQL reference for creating your AWS IoT rules.

Data

In this rule, we create a message composed of two data elements: device_id and temperature.

  • temperature: Use the data of the same name contained in the message that was the subject of the rule.
  • device_id: Use the second string from the topic name split with slashes using the topic function.

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)

FROM Clause

In the FROM clause, we specify the topic name from which to retrieve data. We target the topic device/+/data.

SQL Version

You can set the SQL version. The “2016-03-23” version we specify is explained by AWS as follows:

2016-03-23 – The SQL version built on 2016-03-23 (recommended).

SQL versions

Republish Action

You can set the level of Quality of Service (QoS) with the Qos property. The levels can be “0” or “1”; we specify “0” this time. AWS explains this level as follows:

Sent zero or more times

This level should be used for messages that are sent over reliable communication links or that can be missed without a problem.

MQTT Quality of Service (QoS) options

We specify device/data/temp as the destination topic for republishing.

IAM Role

Detail of IAM Role 01.
Detail of IAM Role 02.

In the trust policy, we allow the iot.amazonaws.com service to assume this IAM role. The IAM policy attached to the role permits the iot:Publish action. We specify the ARN of the destination topic for republishing, allowing messages to be sent to this topic.

CloudFormation Template

AWS IoT Rule

Resources:
  TopicRule:
    Type: AWS::IoT::TopicRule
    Properties:
      RuleName: republish_temp
      TopicRulePayload: 
        Actions: 
          -
            Republish: 
              Qos: 0
              RoleArn: !GetAtt TopicRuleRole.Arn
              Topic: !Ref DestTopicName
        AwsIotSqlVersion: 2016-03-23
        RuleDisabled: false
        Sql: !Sub "SELECT topic(2) as device_id, temperature FROM '${SourceTopicName}'"
Code language: YAML (yaml)

IAM Role

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:
                  - iot:Publish
                Resource:
                  - !Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topic/${DestTopicName}"
Code language: YAML (yaml)

Full Template

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

Verification

Now that everything is set up, we will verify the operation by accessing the MQTT test client page.

First, subscribe to two topics:

  • device/+/data
  • device/data/temp
Detail of AWS IoT 3.

Next, publish a message to the topic device/22/data.

Detail of AWS IoT 4.

We can receive the message on device/+/data. This topic uses wildcard notation and can receive the message we just published.

Now, check device/data/temp.

Detail of AWS IoT 5.

A message has also arrived here. AWS IoT Rules republished the message to this topic.

Looking at the content of the message, it includes two data elements: device_id and temperature. The former is part of the topic name, and the latter is data included in the original message before republishing.

Conclusion

By using AWS IoT Rules, you can republish messages. By properly configuring the SQL expression, which is the essence of the rule, you can extract and process the content of the messages to be republished. This enables efficient management and utilization of IoT data.

TOC