Create IoT rule

AWS (Amazon Web Services) IoT rules can do many specific activities when your MQTT messagge arrives. E.g. store message to database or run Lambda function and much more..

In this post we will create simple rule which store message to Dynamo Database. As a prerequisite you have to have a IoT thing which we described in this post: http://logimic.org/connect-iot-device-to-aws/

IoT rule storing MQTT message to DynamoDB

Login to AWS console portal and go to IoT Core service. In left menu select Act and click Create rule button.

On next screen define the name of rule an description and keep Rule query statement as it is e.g. SELECT * FROM ‘iot/topic’.

Then in section Set one or more actions click Add action button.

In next screen select Insert a message into a DynamoDB table and then click Configure action button.

Now we have to configure action. Because we do not have any DynamoDB yet, click Create a new resource button.

This opens new window of DynamoDB resources where you can create new table. Simply click Create table button.

On next screen specify Table name and Primary key. Primary key is a first column of the table identifying each entry (row). Then click Create button.

That’s it, after a short while the table is ready

Now go back to previous window with configuring of Iot rule. Here click small refresh button and then select your new Dynamo table from popup menu. Notice that Partition key has been filled out.

Enter Partition key value which can be variable or constant, let’s enter “name” as the constant. Enter “payload” as a column where data should be written. Now click CreateRole.

Specify Name of new role and click Create role, then Add action.

Now you can see the action in the page of Iot rule. Click in the bottom of page Create rule button.

Now you can see your new IoT rule in the dashboard of rules.

Time to test it

In left menu click Test which brings you to AWS integrated MQTT client. There select Publish to a topic item which scrolls you to publish section.

Here specify MQTT topic which in our case should be “iot/topic” because we used it in rule specification above. The body of topic can be arbitrary JSON message. Then click Publish to topic button.

Now go to DynamoDB and your table, click refresh button and you should see one record here in payload column. Click on that and you can populate the content.

Notice one thing. When you try to send the same IoT message again and again with the same topic but different content, the row in DynamoDB is still rewritten. In next chapter we will show you how to insert each MQTT message as new entry into DynamoDB.

Identifier of rows

The charm is in unique identifier of rows which must be unique for each row otherwise the entry is rewritten. So let’s go to your Iot Rule by clicking on it.

Then click Edit at Insert a message into a DynamoDB table in Actions field.

Now modify Partition key value and enter here a variable “name” (it can have any other name you like to). The field syntax is ${name}. This means that your message must contain key “name” and its value will be taken as identifier of row. Then click Update button.

Now try to publish MQTT messages with the same topic, but including key “name”. Publish each message with new unique value of the key.

Now go to DynamoDB and you can see here all your messages. Each message in new row. If you send more messages with the same “name” identifier, the the given row in DynamoDB will be rewritten.

So that’s it and have happy MQTT data storing 🙂