Optimizing AWS IoT Costs with Basic Ingest: Utilizing Ingest Topics and Publishing Messages

0

I've been exploring AWS IoT Basic Ingest as a means to reduce AWS IoT message broker costs. Despite referencing the official AWS documentation and attempting to configure it as per the instructions provided, I've encountered some roadblocks. The configured setup doesn't seem to be functioning as expected.

I've encountered the recommended topic format $aws/rules/<rule-name>/<rest-of-topic>. In my scenario, I plan to publish messages on a topic like $aws/rules/routeToKinesis/Building1/room2/tubelight, associating it with the rule named routeToKinesis. Yet, I'm seeking clarification on the precise construction of the SQL statement within the rule. Could you provide guidance on structuring the SQL statement to adeptly interpret and process incoming messages conforming to this specific topic structure?

Additionally, I am keen to understand how to publish messages to the ingest topics within AWS IoT Basic Ingest. What are the recommended approaches, necessary SDKs, or tools needed to accomplish this task efficiently? Detailed guidance, code examples, or a step-by-step walkthrough would be invaluable at this point.

I would greatly appreciate your assistance in shedding light on the best practices and effective steps for utilizing AWS IoT Basic Ingest to optimize costs. Are there any specific nuances or troubleshooting steps that I should be aware of to resolve this issue? Thank you for your help!

asked 5 months ago292 views
2 Answers
1

Here are a few suggestions to help configure AWS IoT Basic Ingest and publish messages to it:

Regarding the topic structure, the recommended format is correct - $aws/rules/<rule-name>/<rest-of-topic>. For your use case, the topic would be $aws/rules/routeToKinesis/Building1/room2/tubelight. I don't know if it was a typo, but do not forget the "rules" element before the rule name in your topic.

As and example, the SQL statement in the rule needs to use a topic() function to extract the building, room and device information from the rest of the topic:

SELECT
  topic(2) as building, 
  topic(3) as room,
  topic(4) as device
FROM
  '$aws/rules/routeToKinesis/Building1/room2/tubelight'

This extracts Building1, room2 and tubelight into separate fields that can be processed by the rule action. You can use any other function to send any part of the payload to downstream systems as usual.

To publish messages to IoT Basic Ingest topics, you can use one of the AWS IoT Device SDKs. Here is some sample Python code to publish a message using the MQTT SDK:

import AWSIoTPythonSDK.MQTTLib as AWSIoTPyMQTT

myAWSIoTMQTTClient = AWSIoTPyMQTT.AWSIoTMQTTClient("basicIngestPublisher")
myAWSIoTMQTTClient.configureEnd[]()point("xxxxxxxxxxxxx.iot.us-east-1.amazonaws.com", 8883)
myAWSIoTMQTTClient.configureCredentials("../../../certs/AmazonRootCA1.pem", "../../../certs/private.pem.key", "../../../certs/certificate.pem.crt")

myAWSIoTMQTTClient.connect()
myAWSIoTMQTTClient.publish("$aws/routeToKinesis/Building1/room2/tubelight", "{\"status\":\"on\"}", 0)

This publishes a simple JSON message to the desired topic.

Overall, when using IoT Basic Ingest:

  • Follow AWS best practices like the Well-Architected Framework pillars to ensure security, reliability, performance, and operational excellence.

  • Monitor metrics like publish success rate to identify any issues.

  • Use IoT Device Defender to secure devices publishing to IoT Basic Ingest.

  • Use IoT Device Management to easily manage fleet of devices.

Resources:

Let me know if you have any other questions!

profile pictureAWS
answered 5 months ago
  • It does not seem to work, Here is my SQL query to read from the published topic :- SELECT * FROM "$aws/rules/routeToKinesis/Building1/room2/tubelight". In the rule action I'm just forwarding the message to another topic to check whether or not this thing is working.

    And here is my policy which allows user to publish and connect { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "iot:Connect", "Resource": "arn:xxxxxxxxxxxxxxxxx:client/${iot:Connection.Thing.ThingName}" }, { "Effect": "Allow", "Action": "iot:Publish", "Resource": "arn:xxxxxxxxxxxxxxxxx:topic/$aws/rules/routeToKinesis/#" } ] }

0

A few issues:

  1. Use of MQTT wildcard characters in policies

See https://docs.aws.amazon.com/iot/latest/developerguide/pub-sub-policy.html#pub-sub-topic-wildcard for guidance. Basically you can only use MQTT wildcards for subscribing to topic filters, so your policy for publishing should include the IAM wildcard * instead of #

  1. In the first answer provided, there is an error in the client code example. You must publish to $aws/rules/<rulename>/<topic> - the author omitted the rules portion.

  2. In the first answer provided, the SQL statement needs improvement. It should read as below:

SELECT
  * as message,
  topic(1) as building, 
  topic(2) as room,
  topic(3) as device
FROM
  '$aws/rules/routeToKinesis/+/+/+'

Note I have include * as message, which will populate the received event with the message content. If you omit this, you'd only receive the topic metadata (building, room, device) in the received event.

E.g. if the following message is sent to $aws/rules/routeToKinesis/Building1/room2/tubelight

{ "foo": "bar" }

the event sent to the rule destination will be formatted as follows:

{
  "building": "Building1",
  "room": "room2",
  "device": "tubelight",
  "message": { "foo": "bar" }
}
profile picture
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions