IoT Events custom JSON payload not working

0

I'm trying to use IoT Events custom payload with Lambda action. When I pass a JSON list format data from input and use it in the Lambda Action with JSON payload the following error occurs:

We couldn't parse your content expression for the payload of LambdaAction. Enter a content expression with the correct syntax.

Code example:

{ lambda: { functionArn: consumer.functionArn, payload: { contentExpression: "{\"deviceId\": \"test\", \"applianceId\": \"another test\"}", type: 'JSON' } } }

The answer from https://repost.aws/questions/QUJ23sjtbVTMSmG4JzITEK1g/io-t-events-custom-payload-error not working. Did something change?

已提問 2 年前檢視次數 699 次
2 個答案
0

I used the kind of content expression you have provided in the question and I could get the desired response with Lambda action.

"{
    \"deviceId\": \"test\", 
    \"applianceId\": \"another test\"
}"

Event received by Lambda is

Received Event is: {'deviceId': 'test', 'applianceId': 'another test'}

For the custom payload expression, you can specify the payload in following format for JSON payloads.

"{
    \"variable1\": \"${$input.my_input.stringVariable}\", 
    \"variable2\": ${$input.my_input.numberVariable}
}"
  • #input.my_input.stringVariable is returning a string, and
  • #input.my_input.numberVariable is returning a number

This translates into the following when the DM is exported or described via API.

"\"{
    \\\"variable1\\\": \\\"${$input.my_input.stringVariable}\\\", 
    \\\"variable2\\\": ${$input.my_input.numberVariable}
}\""

May be you could try double-escaping the string values for creating the detector model from CLI.

AWS
已回答 2 年前
0

Maybe this one will help your IoT Event. It seems like there is an issue with the syntax of your content expression in the payload for the Lambda action. From the code example you provided, it appears that you are using a JSON object as the value for the contentExpression property.

To fix the issue, you can try updating your code as follows: { lambda: { functionArn: consumer.functionArn, payload: { contentExpression: "{"content": {"deviceId": "test", "applianceId": "another test"}}", type: 'JSON' } } }

In this updated code, I've added an additional level of nesting to the contentExpression value to ensure it represents a valid JSON object. The payload now has a "content" property which contains the device and appliance IDs.

Make sure to adjust the contentExpression property according to your desired JSON payload structure.

已回答 9 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南