Recently been updating my jobs Lambda code to nodejs18.x in response to the deprecations coming this summer and took the opportunity to upgrade the aws sdk version to 3 since the 18.x runtime doesn’t come with sdk v2 by default. This is mostly a seamless transition, but the Lambda Client and invoke command are causing some very inconsistent behaviors I’m unable to solve. After finding out that the v3 lambda client returns an int8array and using a buffer to decode the response, I still sometimes receive this error:
Syntax Error: Unexpected token , in JSON at position 3
This sometimes comes in as an invoke error
with the same message but the lambda invoke documents don’t have any information for troubleshooting this.
The code:
const data = await lambdaClient.send(new InvokeCommand(params));
let response = JSON.parse(Buffer.from(data.Payload).toString());
When using SAM LOCAL INVOKE
this same payload on the same code always works.
So my question is, what would cause the JSON to become un-parsable?