Developer Guide

Creating an Amazon DynamoDB Rule

DynamoDB rules allow you to take information from an incoming MQTT message and write it to a DynamoDB table.

To create a DynamoDB rule

  1. In the AWS IoT console, in the navigation pane, choose Act.

  2. On the Rules page, choose Create.

  3. On the Create a rule page, enter a name and description for your rule.


    We do not recommend the use of personally identifiable information in your rule names or descriptions.

  4. Under Rule query statement, choose the latest version from the Using SQL version list. For Rule query statement, enter:

    SELECT * FROM 'my/greenhouse'

    ("SELECT *" specifies that you want to send the entire MQTT message that triggered the rule. "FROM 'my/greenhouse'" tells the rules engine to trigger this rule when an MQTT message is received whose topic matches this topic filter. Choose Add action.

  5. On the Select an action page, choose Insert a message into a DynamoDB table, and then choose Configure action.

  6. On the Configure action page, choose Create a new resource.

  7. On the Amazon DynamoDB page, choose Create table.

  8. On the Create DynamoDB table page, enter a name in Table name. In Partition key, enter Row. Select Add sort key, and then enter PositionInRow in the Sort key field. Row represents a row of plants in a greenhouse. PositionInRow represents the position of a plant in the row. Choose String for both the partition and sort keys, and then choose Create. It takes a few seconds to create your DynamoDB table. Close the browser tab where the Amazon DynamoDB console is open. If you don't close the tab, your DynamoDB table is not displayed in the Table name list on the Configure action page of the AWS IoT console.

  9. On the Configure action page, choose your new table from the Table name list. In Partition key value, enter ${row}. This instructs the rule to take the value of the row attribute from the MQTT message and write it into the Row column in the DynamoDB table. In Sort key value, enter ${pos}. This writes the value of the pos attribute into the PositionInRow column. Leave Write message data to this column blank. By default, the entire message is written to a column in the table named Payload. Choose Create a new role.

  10. In Create a new role, enter a unique name in Name, and then choose Create role.

  11. Choose Add action.

  12. Choose Create rule to create your rule.

Testing an Amazon DynamoDB Rule

  1. To test the rule, open the AWS IoT console and from the navigation pane, choose Test.

  2. Choose Publish to a topic. In the Publish section, enter my/greenhouse as the topic. In the message area, enter the following JSON:

    { "row" : "0", "pos" : "0", "moisture" : "75" }

    Return to the DynamoDB console and choose Tables.

    Select the GreenhouseTable and then choose the Items tab. Your data is displayed on the Items tab.