AWS Lambda Serverless endpoint exits without executing function

155 views Asked by At

We have a POST endpoint in our serverless api which listens to a Magento 2 integration activation callback and processes the payload. The Content-Type of this callback request is application/x-www-form-urlencoded. However, when we try to get the callback, the lambda function finishes execution immediately, skipping the entire function body. What we see in the Cloudwatch logs is only this. Not even console.logs are printed. (the endpoint only prints a string to the console. No async operations are in place. Yet this problem persists)

2020-12-12T12:24:47.012+05:30 START RequestId: 4afba03d-54ef-4b5e-bd44-157b0b7a9f9b Version: $LATEST
2020-12-12T12:24:47.050+05:30 END RequestId: 4afba03d-54ef-4b5e-bd44-157b0b7a9f9b
2020-12-12T12:24:47.050+05:30 REPORT RequestId: 4afba03d-54ef-4b5e-bd44-157b0b7a9f9b    Duration: 37.83 ms  Billed Duration: 38 ms  Memory Size: 128 MB Max Memory Used: 109 MB Init Duration: 893.79 ms    

When we try to hit the same endpoint from POSTMAN with Content-Type: application/json, the endpoint works as expected.

Therefore we thought that the problem might be the Content-Type header and read somewhere that adding request mapping templated would solve this problem. Therefore, we even added a mapping template for content type application/x-www-form-urlencoded in the integration request of the lambda method with following content, time to time. But our problem was not solved unfortunately.

"{ "body": "$util.base64Decode($input.body)" }"
{
"formparams" : $input.json('$')
}
{
    "body" : $input.json('$')
}

My question is: How we can set the endpoint to print the POST request payload, preventing it from immediate exiting? We have been searching for a solution to this problem since a week. It would be a great help, if someone can input their helpful, valuable suggestions to solve this problem. Thanks in advance

1

There are 1 answers

0
Pavindu On BEST ANSWER

Since the Content-Type of the Magento 2 Integration activation callback is application/x-www-form-urlencoded, the lambda event for that POST request was something like this.

console.log(event) -> {body: "a=var&b=other_var&c=another_var"}

The endpoint didn't even print anything because I had put console.log(JSON.parse(event. body)). This results in a JSON parse error and the endpoint immediately finishes execution.

When I started parsing the query parameter event body instead of JSON.parse(), the problem was solved.