Creating Amazon IoT rules to route device data to other services - Amazon IoT Core
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Creating Amazon IoT rules to route device data to other services

These tutorials show you how to create and test Amazon IoT rules using some of the more common rule actions.

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

We recommend that you try these in the order they are shown here, even if your goal is to create a rule that uses a Lambda function or something more complex. The tutorials are presented in order from basic to complex. They present new concepts incrementally to help you learn the concepts you can use to create the rule actions that don't have a specific tutorial.

Note

Amazon IoT rules help you send the data from your IoT devices to other Amazon services. To do that successfully, however, you need a working knowledge of the other services where you want to send data. While these tutorials provide the necessary information to complete the tasks, you might find it helpful to learn more about the services you want to send data to before you use them in your solution. A detailed explanation of the other Amazon services is outside of the scope of these tutorials.

Tutorial scenario overview

The scenario for these tutorials is that of a weather sensor device that periodically publishes its data. There are many such sensor devices in this imaginary system. The tutorials in this section, however, focus on a single device while showing how you might accommodate multiple sensors.

The tutorials in this section show you how to use Amazon IoT rules to do the following tasks with this imaginary system of weather sensor devices.

  • Tutorial: Republishing an MQTT message

    This tutorial shows how to republish an MQTT message received from the weather sensors as a message that contains only the sensor ID and the temperature value. It uses only Amazon IoT Core services and demonstrates a simple SQL query and how to use the MQTT client to test your rule.

  • Tutorial: Sending an Amazon SNS notification

    This tutorial shows how to send an SNS message when a value from a weather sensor device exceeds a specific value. It builds on the concepts presented in the previous tutorial and adds how to work with another Amazon service, the Amazon Simple Notification Service (Amazon SNS).

    If you're new to Amazon SNS, review its Getting started exercises before you start this tutorial.

  • Tutorial: Storing device data in a DynamoDB table

    This tutorial shows how to store the data from the weather sensor devices in a database table. It uses the rule query statement and substitution templates to format the message data for the destination service, Amazon DynamoDB.

    If you're new to DynamoDB, review its Getting started exercises before you start this tutorial.

  • Tutorial: Formatting a notification by using an Amazon Lambda function

    This tutorial shows how to call a Lambda function to reformat the device data and then send it as a text message. It adds a Python script and Amazon SDK functions in an Amazon Lambda function to format with the message payload data from the weather sensor devices and send a text message.

    If you're new to Lambda, review its Getting started exercises before you start this tutorial.

Amazon IoT rule overview

All of these tutorials create Amazon IoT rules.

For an Amazon IoT rule to send the data from a device to another Amazon service, it uses:

  • A rule query statement that consists of:

    • A SQL SELECT clause that selects and formats the data from the message payload

    • A topic filter (the FROM object in the rule query statement) that identifies the messages to use

    • An optional conditional statement (a SQL WHERE clause) that specifies specific conditions on which to act

  • At least one rule action

Devices publish messages to MQTT topics. The topic filter in the SQL SELECT statement identifies the MQTT topics to apply the rule to. The fields specified in the SQL SELECT statement format the data from the incoming MQTT message payload for use by the rule's actions. For a complete list of rule actions, see Amazon IoT Rule Actions.