Skip to content

AWS IoT Rule Query - decode() on raw binary protobuf

0

I am having some issues with getting the decode() function working for raw binary protobuf. I am sending raw binary protobuf chunks over MQTT, and I am seeing them received through the MQTT test client. However, my IoT Rule Query is failing due to the following issue from the CloudWatch AWSIotLogsV2 log group:

{
    "timestamp": "2025-11-15 05:18:39.302",
    "logLevel": "ERROR",
    "traceId": "d8f6e706-478b-c777-2a51-793a7caeb830",
    "accountId": "372080370602",
    "status": "Failure",
    "eventType": "RuleExecution",
    "clientId": "poc-SHAUserIdSHADeviceId",
    "topicName": "sdk/test/java/chunk/12",
    "ruleName": "heart_rate_protobuf_rule",
    "principalId": "8d322e1354a0630e5a2dc0a2e4da997bde04ba34f74e20557b5bc0b09f049628",
    "reason": "JsonParseError",
    "details": "Error parsing message for rule 'heart_rate_protobuf_rule'"
}

This is my SQL statement:

SELECT *, decode(*, 'proto', 'aws-iot-annlin', 'heartrate.desc', 'heartrate', 'HeartRateChunk') as decoded_chunk FROM 'sdk/test/java/chunk/+'

My understanding from the available documentation is that I can decode a raw binary protobuf incoming payload using this format: decode(*, 'proto', ...). I am not sure what I am missing and why I am getting the JsonParseError. That seems to indicate to me that by the time my raw binary protobuf reaches the IoT rule, it is getting interpreted as JSON somehow.

I did test locally that my .desc can correctly interpret my raw binary protobuf chunk.

Sources I am referencing: https://docs.aws.amazon.com/iot/latest/developerguide/binary-payloads.html#binary-payloads-protobuf https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html#iot-sql-decode-base64

asked a year ago259 views

2 Answers
0
Accepted Answer

The error you're encountering suggests that AWS IoT Rules Engine is trying to parse your binary protobuf message as JSON before applying the decode function, which is causing the JsonParseError.

When working with raw binary protobuf payloads in AWS IoT Core rules, you need to understand that the Rules Engine first attempts to parse the incoming message as JSON before applying any SQL functions. For binary data, this parsing will fail.

To properly handle binary protobuf messages, you should modify your SQL statement to only use the decode function without trying to select other fields. Try using this SQL statement instead:

SELECT decode(*, 'proto', 'aws-iot-annlin', 'heartrate.desc', 'heartrate', 'HeartRateChunk') as decoded_chunk FROM 'sdk/test/java/chunk/+'

Notice that I've removed the *, from the beginning of your SELECT statement. This is because you can't select both the raw binary data and the decoded data in the same query when working with binary payloads.

Also, it's important to note that error actions in IoT rules are only triggered when an action fails, not when the SQL statement itself fails. If your SQL statement fails due to parsing errors, the error action won't be executed because the rule engine never gets to the action execution phase.

Make sure your descriptor files are correctly uploaded to the specified S3 bucket and that the AWS IoT role has permissions to access them. The decode function requires the following parameters:

answered a year ago

AWS
EXPERT

reviewed a year ago

0

re:Post Agent identified the issue with my SQL query. This is what fixed it:

To properly handle binary protobuf messages, you should modify your SQL statement to only use the decode function without trying to select other fields. Try using this SQL statement instead:

SELECT decode(*, 'proto', 'aws-iot-annlin', 'heartrate.desc', 'heartrate', 'HeartRateChunk') as decoded_chunk FROM 'sdk/test/java/chunk/+'

answered a year ago

AWS
EXPERT

reviewed a year 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.