无法从AWS IoT规则触发aws_lambda函数

huangapple go评论52阅读模式
英文:

Unable to trigger aws_lambda function from AWS IoT Rule

问题

我正在尝试使用AWS IoT规则处理SMILE编码格式的有效负载,该有效负载来自IoT设备,并使用Snappy压缩发布到Kafka主题。

我使用以下SQL查询设置了AWS IoT规则,它从IoT设备接收SMILE编码格式的有效负载,并以JSON格式发布到Kafka主题,而无需解码。这个工作正常。

SQL查询

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

我们从IoT设备接收到的有效负载如下

kqIY8G06KQoB+oNAZHRvQkJhdIRpdGVtc

在不解码的情况下将有效负载发布到Kafka主题的JSON格式如下:

{
"payload": "kqIY8G06KQoB+oNAZHRvQkJhdIRpdGVtc"
}

但是在AWS IoT规则中发布到Kafka主题之前,我需要解码有效负载。因此,我创建了AWS Lambda函数来解码SMILE编码的有效负载。

为了触发AWS Lambda函数,我使用了此链接中的aws_lambda函数 - https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html#iot-func-aws-lambda。它需要两个参数,第一个参数是创建的AWS Lambda函数ARN,第二个参数是以JSON格式输入到Lambda函数的输入。

要将输入传递给AWS Lambda函数,我们需要以JSON格式提取有效负载。我尝试了以下SQL查询以构建JSON格式并将其传递给AWS Lambda函数以解码有效负载值,但Lambda函数没有触发。

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

如果我修改下面提到的SQL查询和IoT设备发布的有效负载,则AWS Lambda函数会触发,但我不想更改IoT设备发布的有效负载结构:

修改后的SQL查询:

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

修改后的有效负载

从:

kqIY8G06KQoB+oNAZHRvQkJhdIRpdGVtc

到:

{
"payload": "kqIY8G06KQoB+oNAZHRvQkJhdIRpdGVtc"
}

我已经花了很多时间但没有成功。我是否遗漏了什么?有人可以帮助我了解为什么AWS Lambda函数没有触发吗?

先行致谢,

Shiva

英文:

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

答案1

得分: 0

以下查询触发了 Lambda 函数,Lambda 函数能够解码有效负载。

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

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'

huangapple
  • 本文由 发表于 2023年6月8日 18:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定