0
votes

I am sending data from Raspberry Pi to AWS IoT core on a specific topic. The data is received fine when checking with the AWS Mqtt client. The problem is with sending the data to a lambda function to do some processing. I created a lambda function to be triggered from AWS IoT with "SELECT * FROM 'farm'" rule statement. But the the IoT logs show this error "Received Server error from Lambda".

{   
"timestamp": "2019-07-22 07:05:18.353",   

"logLevel": "ERROR",

"traceId": "023554fc-c1e0-8f81-9337-d1106908c007",

"accountId": "************",

"status": "Failure",

"eventType": "RuleExecution",

"clientId": "$GEN/d211c7a6-39b3-****-****-************",

"topicName": "farm",

"ruleName": "iotTOlambda",

"ruleAction": "LambdaAction",

"resources": {

"FunctionArn": "arn:aws:lambda:eu-central-1:************:function:FarmWritingOnDynamoDP"
    },

 "principalId": "e14e3667fc136e44556535247393daad8a8c9265a1eaa04d3d9184cd65e4c961",

 "details": "Received Server error from Lambda"
 }

However, the weird thing is when I send the same data through the Mqtt client publish the lambda get invoked successfully.

I tried checking the lambda execution time but its fine. Also, I looked at the type of data but I received the same error.

This is the code

     from __future__ import print_function
     import boto3
     import json
     print('Loading function')
     def handler(event, context):
         print(event)
         #Some data Processing

Here the expected result to print the received event, but instead I received the error "Received Server error from Lambda"

For the lambda role, I used the AWSLambdaBasicExecutionRole

{ "Version": "2012-10-17",
     "Statement": [
        {
           "Effect": "Allow",
           "Action": "logs:CreateLogGroup",
           "Resource": "arn:aws:logs:eu-central-1:725297230163:*"
        },
    {
        "Effect": "Allow",
        "Action": [
            "logs:CreateLogStream",
            "logs:PutLogEvents"
        ],
        "Resource": [
            "arn:aws:logs:eu-central-1:725297230163:log-group:/aws/lambda/FarmWritingOnDynamoDP:*"
        ]
    }
]
}
1

1 Answers

0
votes

When your rule activates, and a message is sent to your lambda function, the lambda function is expecting to receive JSON. Most likely, the MQTT message you are receiving from the Pi and then redirecting is not in JSON.

If you look at the AWS Lambda API for the action invoke, it gives some specifics on the error messages you received.

InvalidParameterValueException One of the parameters in the request is invalid.

HTTP Status Code: 400

InvalidRequestContentException The request body could not be parsed as JSON.

HTTP Status Code: 400