Search code examples
aws-lambdaaws-iot

Unable to trigger aws_lambda function from AWS IoT Rule


I am experimenting AWS IoT rule which receives payload in SMILE encoding format with snappy compressed from IoT devices and publishes it to the Kafka topic.

I set up the AWS IoT rule with the below SQL query, it receives payload in SMILE encoding format from IoT devices and publishes it to the Kafka topic in JSON format without decoding. This works fine.

SQL query

SELECT decode(encode(*,'base64'),'base64') as payload FROM 'mqtt_topic'

The below payload which we received from the IoT devices

kqIY8G06KQoB+oNAZHRvQkJhdIRpdGVtc

The published payload to Kafka topic in JSON format without decoding:

{
"payload": "kqIY8G06KQoB+oNAZHRvQkJhdIRpdGVtc"
}

But I need to decode the payload before publishing it to the Kafka topic in the AWS IoT rule. So I created the AWS lambda function in Java to decode the SMILE encoded payload.

To trigger the AWS lambda function, I used the aws_lambda function from this link - https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html#iot-func-aws-lambda. It takes two arguments, 1st argument is the created AWS Lambda function ARN and 2nd argument is input to the lambda function in JSON format.

To pass input to the AWS lambda function, we need a payload in JSON format. I tried below SQL query to construct in JSON format and pass it to the AWS lambda function to decode the payload value but the lambda function is not triggering.

SELECT value aws_lambda("arn:aws:lambda:region:account-id:function:DeserializerFunction", {"payload": decode(encode(*,'base64'),'base64')}) from 'mqtt_topic'

If I modify the below mentioned SQL query and the payload which IoT devices are publishing, then the AWS lambda function is triggering but I do want to change the payload structure which IoT devices are publishing:

Modified SQL query:

SELECT value aws_lambda("arn:aws:lambda:region:account-id:function:DeserializerFunction", *) from 'mqtt_topic'

Modified payload

From:

kqIY8G06KQoB+oNAZHRvQkJhdIRpdGVtc

To:

{
"payload": "kqIY8G06KQoB+oNAZHRvQkJhdIRpdGVtc"
}

Spent so much time on this but no luck.

Am I missing something? Could anyone please help me why the AWS lambda function not triggering?

Thanks in Advance,

Shiva


Solution

  • The below query triggers the lambda function and the lambda function is able to decode the payload.

    SELECT value aws_lambda("arn:aws:lambda:region:account-id:function:DeserializerFunction", {"payload": encode(*,'base64')}) from 'mqtt_topic'