Working with IoT rules

One of the most fascinating features recently provided with the AWS IoT Core is IoT rules. With IoT rules, you can basically provide your connected devices with the ability to interact with other AWS services. IoT rules provide a predefined set of rules that allow you to perform a variety of tasks, such as those listed here:

  • Write data received from a device to an Amazon DynamoDB table
  • Send a push notification to all users using Amazon SNS
  • Publish data to an Amazon SQS queue
  • Invoke a Lambda function to perform some data transformation
  • Process data from devices using Amazon Kinesis, and much more

An IoT rule also provides you with an added functionality which enables you to query and filter device data, as well use simple SQL commands. Based on the SQL statement execution, you can then either invoke a success or an error action:

  1. To get started with IoT rules, select the Act option from the AWS IoT Core console. Since this is our first time, click on Create a rule to proceed with the next steps.
  2. On the Create a rule page, start off by providing a suitable Name and a Description for your rule. In this scenario, we will be creating an IoT rule that will write all device data to a DynamoDB table.
  3. Next, from the Using SQL version drop-down list, select an appropriate SQL version for this rule. By default, the 2016-03-23 will be selected.
  4. Next, we form the Rule query statement. This is used to filter out particular messages from a large set of device data. In this case, we want all the data from our demo device to be written to a DynamoDB table, so in the Attribute field, type in an * which indicates all fields followed by a valid topic name in the Topic filter option. You can also set an optional Condition to match your query.

Here's what the final query should look like:

  1. With the query in place, the next step is to associate one or more actions with your IoT rule. From the Set one or more actions section, select the Add action option to get started.
  2. On the Select an action page, you can browse and select one of the predefined action templates. In this case, we will be selecting the Insert a message into a DynamoDB table action, which allows you to write all, or part of, an MQTT message to a DynamoDB table. Once selected, click on the Configure action to proceed.
  3. This will take you to the Configure action page where you can either select an existing DynamoDB table, or alternatively create one as well. For this use case, we will be relying on the UUID and state code that we last executed on our sample device. In order to split and write the message data to their individual columns, ensure that the DynamoDB table that you create matches the following settings:
    • Partition key: uuid
    • Sort key: state
    • Read capacity units: 5
    • Write capacity units: 5
  1. Once the table is created, select it from the Table name drop-down list. Next, fill in the corresponding expressions in the Hash key value and Range key value fields, as depicted in the following screenshot. These expressions will write the message values into their corresponding DynamoDB table columns:
  1. Ensure that you also create and assign an IAM Role that will grant write AWS IoT access to the DynamoDB table. With the table and IAM Role assigned, click on Add action to complete configuring the action. You can correspondingly use the same process to add multiple actions to a single IoT rule. For example, write the device messages to a DynamoDB table as well as to an SQS queue, and so on. With the rule ready, click on Create rule to complete the process.
  2. Now, to test the rule. Run the uuid-state code that we ran in the earlier section. Ensure that the topic names in both the code as well as in the IoT rule match, otherwise the code will simply not write anything to the DynamoDB table:
# node test4.js

With the code executing, check the DynamoDB table for the data. You should see a similar output as shown in the following screenshot:

With this, we come to the end of AWS IoT Core. Make sure you clean up and delete the DynamoDB table once the testing completes, to avoid any unnecessary costs. In the next section, we will be exploring yet another powerful AWS IoT service called AWS Greengrass.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset