Creating a rule with a DynamoDB action - AWS IoT Core

Creating a rule with a DynamoDB action

The DynamoDB action allows 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.

    Note

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

    1. 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 initiated the rule.

      "FROM 'my/greenhouse'" tells the rules engine to initiate this rule when an MQTT message whose topic matches this topic filter is received.

    2. Choose Add action.

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

  5. On Configure action, choose Create a new resource.

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

    1. On Create DynamoDB table, enter a name. In Partition key, enter Row.

    2. 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.

    3. Choose String for both the partition and sort keys, and then choose Create. It takes a few seconds to create your DynamoDB table.

    4. 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.

    1. On Configure action, choose your new table from the Table name list.

    2. 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.

    3. In Sort key value, enter ${pos}. This writes the value of the pos attribute into the PositionInRow column.

    4. In Write message data to this column, enter Payload. This inserts the message payload into the Payload column.

    5. Leave Operation blank. Because you specified a value in the Write message data to this column field, the Operation must be blank. This field allows you to specify which operation (INSERT, UPDATE, or DELETE) you want to perform when the action is initiated; however, entering Payload in Write message data to this column indicates that this rule action will perform an INSERT operation.

    6. Choose Create a new role.

  7. In Create a new role, enter a unique name, and then choose Create role.

  8. Choose Add action.

  9. Choose Create rule.

Testing a rule with a DynamoDB action

  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. 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 Items. Your data is displayed on the Items tab.