Developer Guide

Creating a 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 left navigation pane, choose Act.

  2. On the Rules page, choose Create.

  3. On the Create a rule page, in the Name field, type a name for your rule. In the Description field, type a description for the rule.

  4. Scroll down to Message source. Choose the latest version from the Using SQL version drop-down list. In the Attribute field, type *. This specifies that you want to send the entire MQTT message that triggered the rule.

  5. The rules engine uses the topic filter to determine which rules to trigger when an MQTT message is received. In the Topic filter field, type iotbutton/your-button-DSN. If you are not using an AWS IoT button, type my/topic or the topic used in the rule.


    You can find the DSN on the bottom of the button.

    Leave Condition blank.

  6. In Set one or more actions, choose Add action.

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

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

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

  10. On the Create DynamoDB table page, type a name in the Table name field. In Partition key, type SerialNumber. Select the Add sort key check box, and then type ClickType in the Sort key field. Select String for both the partition and sort keys.

  11. 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 do not close the tab, your DynamoDB table will not be displayed in the Table name drop-down list on the AWS IoT Configure action page.

  12. On the Configure action page, choose your new table from the Table name drop-down list. In Hash key value, type ${serialNumber}. This instructs the rule to take the value of the serialNumber attribute from the MQTT message and write it into the SerialNumber column in the DynamoDB table. In Range key value, type ${clickType}. This writes the value of the clickType attribute into the ClickType 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.

  13. Type a unique name in IAM role name, and then choose the Create a new role button again. Choose the role you just created, choose Update role, and then choose Add action.

  14. Choose Create rule to create the rule.

  15. A confirmation message shows the rule has been created. Choose the left arrow to return to the Rules page.

  16. Test the rule by pressing your configured AWS IoT button or by using an MQTT client to publish a message on a topic that matches your rule's topic filter. Finally, return to the DynamoDB console and select the table you created to view the entry for your button press or message.