Working with binary payloads - AWS IoT Core

Working with binary payloads

When the message payload should be handled as raw binary data, rather than a JSON object, use the * operator to refer to it in a SELECT clause. This works for non-JSON payloads with some rule actions, such as the S3 action.

Binary payload examples

When you use * to refer to the message payload as raw binary data, you can add data to the rule. If you have an empty or a JSON payload, the resulting payload can have data added using the rule. The following shows examples of supported SELECT clauses.

  • You can use the following SELECT clauses with only a * for binary payloads.

    • SELECT * FROM 'topic/subtopic'
    • SELECT * FROM 'topic/subtopic' WHERE timestamp() % 12 = 0
  • You can also add data and use the following SELECT clauses.

    • SELECT *, principal() as principal, timestamp() as time FROM 'topic/subtopic'
    • SELECT encode(*, 'base64') AS data, timestamp() AS ts FROM 'topic/subtopic'
  • You can also use these SELECT clauses with binary payloads.

    • The following refers to device_type in the WHERE clause.

      SELECT * FROM 'topic/subtopic' WHERE device_type = 'thermostat'
    • The following is also supported.

      { "sql": "SELECT * FROM 'topic/subtopic'" "actions": [{ "republish": { "topic":"device/${device_id}" } }] }

The following rule actions don't support binary payloads so you must decode them.

  • Some rule actions don't support binary payload input, such as a Lambda action, so you must decode binary payloads. The Lambda rule action can receive binary data, if it's base64 encoded and in a JSON payload. You can do this by changing the rule to the following.

    SELECT encode(*, 'base64') AS data FROM 'my_topic'
  • The SQL statement doesn't support string as input. To convert a string input to JSON, you can run the following command.

    SELECT decode(encode(*, 'base64'), 'base64') AS payload FROM 'topic'