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.

To use * to refer to the message payload as raw binary data, follow these rules:

  1. The SQL statement and templates must not refer to JSON names other than *.

  2. The SELECT statement must have * as the only item, or must have only functions. See the following example.

    SELECT * FROM 'topic/subtopic'
    SELECT encode(*, 'base64') AS data, timestamp() AS ts FROM 'topic/subtopic'

For rule actions that don't support binary payload input, such as Lambda action, 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:

SELECT encode(*, 'base64') AS data FROM 'my_topic'

Binary payload examples

You can use the following SELECT clause with binary payloads because it doesn't refer to any JSON names.

SELECT * FROM 'topic/subtopic'

You cannot use the following SELECT with binary payloads because it refers to device_type in the WHERE clause.

SELECT * FROM 'topic/subtopic' WHERE device_type = 'thermostat'

You cannot use the following SELECT with binary payloads because it violates rule #2.

SELECT *, timestamp() AS timestamp FROM 'topic/subtopic'

You can use the following SELECT with binary payloads because it complies with rule #1 or rule #2.

SELECT * FROM 'topic/subtopic' WHERE timestamp() % 12 = 0

You cannot use the following AWS IoT rule with binary payloads because it violates rule #1.

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