As we have already seen getting started with AWS IoT and implementing the AWS IoT with MQTT in previous blog posts, here we are going to see how to use AWS IoT Data Plane client which comes with the AWS SDK for publishing the payloads to the things.
It implements a broker for applications and things to publish messages over HTTP (Publish) and retrieve, update, and delete thing shadows. A thing shadow is a persistent representation of your things and their state in the AWS cloud.
For using this IoT Data Plane in Rails we need ‘aws-sdk’ gem so, we can start by installing the gem.
Creating a client:
We can create an AWS IoTDataPlane client with AWS credentials as below:
Or, we can configure AWS with credentials by creating an Initializer as below:
Make sure to set the correct region and endpoint of AWS IoT Thing..
Publishing to a topic:
After connecting the client we can publish the payload to the topic as below:
AWS IoT Rules:
An AWS IoT rule consists of a SQL SELECT statement, a topic filter, and a rule action. Devices send information to AWS IoT by publishing messages to MQTT topics. The SQL SELECT statement allows you to extract data from an incoming MQTT message. The topic filter of an AWS IoT rule specifies one or more MQTT topics. The rule is triggered when an MQTT message is received on a topic that matches the topic filter. Rule actions allow you to take the information extracted from an MQTT message and send it to another AWS service. Rule actions are defined for AWS services like Amazon DynamoDB, AWS Lambda, Amazon SNS, and Amazon S3.
AWS IoT rule actions are used to specify what to do when a rule is triggered. The following actions are supported:
- cloudwatchAlarm to change a CloudWatch alarm.
- cloudwatchMetric to capture a CloudWatch metric.
- dynamoDB to write data to a DynamoDB database.
- elasticsearch to write data to a Amazon Elasticsearch Service domain.
- kinesis to write data to a Amazon Kinesis stream.
- lambda to invoke a Lambda function.
- s3 to write data to a Amazon S3 bucket.
- sns to write data as a push notification.
- firehose to write data to an Amazon Kinesis Firehose stream.
- sqs to write data to an SQS queue.
- republish to republish the message on another MQTT topic.
Creating AWS IoT Rule:
From the AWS IoT dashboard you can create an IoT Rule where, you can specify your own SQL query through which you can filter the messages that is published to the topic.
Specify ‘*’ in attribute field to select all attributes or specify the particular attribute you need to retrieve and send to an action.
Specify the topic from which the payloads to be selected.
After filling these fields you can see the final SQL SELECT query in the Rule query statement field.
Then, you can choose one or more actions that need to be performed with the received message.
Using IoT Rule to send messages to backend server:
Let us consider we need to subscribe to a topic and we need to do some process with the received messages on that topic in our Rails backend. For this scenario we can set the IoT rule action as SQS which send the filtered messages to an SQS queue.
We can create a worker which process the messages received in the queue. This is better because all the processing of the messages are done in background.
Thus, we have seen setting up IoT in AWS and using those credentials from AWS to connect with MQTT to perform publish/subscribe to the topics and in these series of blogs, we have seen how to use AWS IoT Dataplane for publish/subscribe operations.