A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.aws.amazon.com/iot/latest/developerguide/iot-ddb-rule.html below:

Tutorial: Storing device data in a DynamoDB table

Tutorial: Storing device data in a DynamoDB table

This tutorial demonstrates how to create an AWS IoT rule that sends message data to a DynamoDB table.

In this tutorial, you create a rule that sends message data from an imaginary weather sensor device to a DynamoDB table. The rule formats the data from many weather sensors such that they can be added to a single database table.

What you'll learn in this tutorial

This tutorial takes about 30 minutes to complete.

Step 1: Create the DynamoDB table for this tutorial

In this tutorial, you'll create a DynamoDB table with these attributes to record the data from the imaginary weather sensor devices:

To create the DynamoDB table for this tutorial
  1. Open the DynamoDB console, and then choose Create table.

  2. In Create table:

    1. In Table name, enter the table name: wx_data.

    2. In Partition key, enter sample_time, and in the option list next to the field, choose Number.

    3. In Sort key, enter device_id, and in the option list next to the field, choose Number.

    4. At the bottom of the page, choose Create.

You'll define device_data later, when you configure the DynamoDB rule action.

Step 2: Create an AWS IoT rule to send data to the DynamoDB table

In this step, you'll use the rule query statement to format the data from the imaginary weather sensor devices to write to the database table.

A sample message payload received from a weather sensor device looks like this:

{
  "temperature": 28,
  "humidity": 80,
  "barometer": 1013,
  "wind": {
    "velocity": 22,
    "bearing": 255
  }
}

For the database entry, you'll use the rule query statement to flatten the structure of the message payload to look like this:

{
  "temperature": 28,
  "humidity": 80,
  "barometer": 1013,
  "wind_velocity": 22,
  "wind_bearing": 255
}

In this rule, you'll also use a couple of Substitution templates. Substitution templates are expressions that let you insert dynamic values from functions and message data.

To create the AWS IoT rule to send data to the DynamoDB table
  1. Open the Rules hub of the AWS IoT console. Or, you can open the AWS IoT homepage within the AWS Management Console and navigate to Message routing>Rules.

  2. To start creating your new rule in Rules, choose Create rule.

  3. In Rule properties:

    1. In Rule name, enter wx_data_ddb.

      Remember that a rule name must be unique within your AWS account and Region, and it can't have any spaces. We've used an underscore character in this name to separate the two words in the rule's name.

    2. In Rule description, describe the rule.

      A meaningful description makes it easier to remember what this rule does and why you created it. The description can be as long as needed, so be as detailed as possible.

  4. Choose Next to continue.

  5. In SQL statement:

    1. In SQL version, select 2016-03-23.

    2. In the SQL statement edit box, enter the statement:

      SELECT temperature, humidity, barometer,
        wind.velocity as wind_velocity,
        wind.bearing as wind_bearing,
      FROM 'device/+/data'

      This statement:

      • Listens for MQTT messages with a topic that matches the device/+/data topic filter.

      • Formats the elements of the wind attribute as individual attributes.

      • Passes the temperature, humidity, and barometer attributes unchanged.

  6. Choose Next to continue.

  7. In Rule actions:

    1. To open the list of rule actions for this rule, in Action 1, choose DynamoDB.

      Note

      Make sure that you choose DynamoDB and not DynamoDBv2 as the rule action.

    2. In Table name, choose the name of the DynamoDB table you created in a previous step: wx_data.

      The Partition key type and Sort key type fields are filled with the values from your DynamoDB table.

    3. In Partition key, enter sample_time.

    4. In Partition key value, enter ${timestamp()}.

      This is the first of the Substitution templates you'll use in this rule. Instead of using a value from the message payload, it will use the value returned from the timestamp function. To learn more, see timestamp in the AWS IoT Core Developer Guide.

    5. In Sort key, enter device_id.

    6. In Sort key value, enter ${cast(topic(2) AS DECIMAL)}.

      This is the second one of the Substitution templates you'll use in this rule. It inserts the value of the second element in topic name, which is the device's ID, after it casts it to a DECIMAL value to match the numeric format of the key. To learn more about topics, see topic in the AWS IoT Core Developer Guide. Or to learn more about casting, see cast in the AWS IoT Core Developer Guide.

    7. In Write message data to this column, enter device_data.

      This will create the device_data column in the DynamoDB table.

    8. Leave Operation blank.

    9. In IAM role, choose Create new role.

    10. In the Create role dialog box, for Role name, enter wx_ddb_role. This new role will automatically contain a policy with a prefix of "aws-iot-rule" that will allow the wx_data_ddb rule to send data to the wx_data DynamoDB table you created.

    11. In IAM role, choose wx_ddb_role.

    12. At the bottom of the page, choose Next.

  8. At the bottom of the Review and create page, choose Create to create the rule.

Step 3: Test the AWS IoT rule and DynamoDB table

To test the new rule, you'll use the MQTT client to publish and subscribe to the MQTT messages used in this test.

Open the MQTT client in the AWS IoT console in a new window. This will let you edit the rule without losing the configuration of your MQTT client. The MQTT client does not retain any subscriptions or message logs if you leave it to go to another page in the console. You'll also want a separate console window open to the DynamoDB Tables hub in the AWS IoT console to view the new entries that your rule sends.

To use the MQTT client to test your rule
  1. In the MQTT client in the AWS IoT console, subscribe to the input topic, device/+/data.

    1. In the MQTT client, choose Subscribe to a topic.

    2. For Topic filter, enter the topic of the input topic filter, device/+/data.

    3. Choose Subscribe.

  2. Now, publish a message to the input topic with a specific device ID, device/22/data. You can't publish to MQTT topics that contain wildcard characters.

    1. In the MQTT client, choose Publish to a topic.

    2. For Topic name, enter the input topic name, device/22/data.

    3. For Message payload, enter the following sample data.

      {
        "temperature": 28,
        "humidity": 80,
        "barometer": 1013,
        "wind": {
          "velocity": 22,
          "bearing": 255
        }
      }
    4. To publish the MQTT message, choose Publish.

    5. Now, in the MQTT client, choose Subscribe to a topic. In the Subscribe column, choose the device/+/data subscription. Confirm that the sample data from the previous step appears there.

  3. Check to see the row in the DynamoDB table that your rule created.

    1. In the DynamoDB Tables hub in the AWS IoT console, choose wx_data, and then choose the Items tab.

      If you're already on the Items tab, you might need to refresh the display by choosing the refresh icon in the upper-right corner of the table's header.

    2. Notice that the sample_time values in the table are links and open one. If you just sent your first message, it will be the only one in the list.

      This link displays all the data in that row of the table.

    3. Expand the device_data entry to see the data that resulted from the rule query statement.

    4. Explore the different representations of the data that are available in this display. You can also edit the data in this display.

    5. After you have finished reviewing this row of data, to save any changes you made, choose Save, or to exit without saving any changes, choose Cancel.

If you don't see the correct behavior, check the troubleshooting tips.

Troubleshooting your DynamoDB rule

Here are some things to check in case you're not seeing the results you expect.

Step 4: Review the results and next steps

After you send a few messages to the DynamoDB table with this rule, try experimenting with it to see how changing some aspects from the tutorial affect the data written to the table. Here are some ideas to get you started.

After you have completed this tutorial, check out Tutorial: Formatting a notification by using an AWS Lambda function.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4