-1

We are setting up the infrastructure in AWS to collect data from IOT devices. Once the devices are registered, they will starting sending json messages to a few MQTT topics. In order to receive the messages and parse them and save the data into a database I plan to create a rule which triggers a Lambda function when a message is received. The Lambda function does the parsing.

Based on the AWS IoT documentation, a rule can be created under IoT to evaluate messages sent by your things with query like SELECT * FROM 'mymsgs/+'. It appears that the rule is not associated with any particular devices. So can I assume it can listen to the topics from all devices under the same account? If that is the case, I can just have one Lambda function to process all the messages that come from different devices.

ddd
  • 4,665
  • 14
  • 69
  • 125

2 Answers2

1

Correct topic rules are not associated with any device. Use the FROM statement to control what messages they receive. You might want to update the SQL statement to

SELECT * as data, topic() as topic FROM mymsgs/+

so that your lambda can know which topic the message was sent on. If a device publishes { foo: "bar", baz: 100 } on topic mymsgs/device then

{
  "data": {
    "foo": "bar",
    "baz": 0
  },
  "topic": "mymsgs/device1"
}

will be send to the lambda function.

You can also use IoT policies attached to thing certificates to enforce that a thing is only publishing on the topics it should.

cementblocks
  • 4,326
  • 18
  • 24
  • Your SELECT statement basically covers all topics, right? And `data` in your SELECT statement will be used as input of the lambda function? – ddd Aug 20 '18 at 17:43
  • I've updated the example. The SELECT clause controls what is send to lambda (or other rule actions), the FROM clause controls which topics using MQTT topic wildcards + and #. The WHERE clause can filter based on data in the payload. – cementblocks Aug 20 '18 at 18:05
0

If the number of topics is less you can do the following

SELECT *, topic() as topic FROM 'mylog/+' where regexp_matches(topic(), 'mylog/\b(info|error|warn)\b') = TRUE
Sudhakar Naidu
  • 244
  • 2
  • 7