Describe the issue
I am invoking my lambda function like so in express.JS TypeScript,
import type {InvokeCommandOutput} from '@aws-sdk/client-lambda';
import {InvokeCommand} from '@aws-sdk/client-lambda';
const command = new InvokeCommand({
FunctionName: this.client_name,
InvocationType: 'RequestResponse',
Payload: Buffer.from(JSON.stringify(event)),
});
const response: InvokeCommandOutput = await this.client.send(command);
const payLoad = response.Payload;
if (payLoad != undefined) {
return response.Payload;
}
Expecting the following response on the InvokeCommandOutput.Payload
object from my lambda function.
{
"challenge_name": "CHALLENGE_NAME",
"session": "session string"
}
. I did this using the following package https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-lambda/index.html
I've also tried the example on AWS as well as the toUtf8
and fromUtf8
functions in the SDK to decode the UINT8Array. However, they give me the following error.
{"errorType":"TypeError","errorMessage":"The \"key\" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received null","trace":["TypeError [ERR_INVALID_ARG_TYPE]: The \"key\" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received null"," at new NodeError (node:internal/errors:372:5)"," at prepareSecretKey (node:internal/crypthe \"key\" argument must be of type string or ano/keys:580:11)"," at new Hmac (node:internal/crypto/hash:132:9)"," at createHmac (node:crypto:162:10)"," at AuthService.initiateAuth (/var/task/main.js:57511:48)"," at processTicksAndRejections (node:internal/process/task_queues:crypto/hash:132:9)"," at createHmac (node:cry96:5)"," at async Runtime.handler (/var/task/main.js:34:9)"]}
Links
Update
When when I replace....
if(payLoad != undefined){
console.info(payLoad);
return payLoad;
}
...with
if(payLoad != undefined){
console.info(payLoad);
return JSON.parse(Buffer.from(payLoad).toString());
}
I get the following payload.
Uint8Array(777) [
123, 34, 101, 114, 114, 111, 114, 84, 121, 112, 101, 34,
58, 34, 84, 121, 112, 101, 69, 114, 114, 111, 114, 34,
44, 34, 101, 114, 114, 111, 114, 77, 101, 115, 115, 97,
103, 101, 34, 58, 34, 84, 104, 101, 32, 92, 34, 107,
101, 121, 92, 34, 32, 97, 114, 103, 117, 109, 101, 110,
116, 32, 109, 117, 115, 116, 32, 98, 101, 32, 111, 102,
32, 116, 121, 112, 101, 32, 115, 116, 114, 105, 110, 103,
32, 111, 114, 32, 97, 110, 32, 105, 110, 115, 116, 97,
110, 99, 101, 32,
... 677 more items
]
...that gives me the following error. Meaning there's an error in the Payload that could not be decoded.
'The "key" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received null'
CodePudding user response:
response.Payload
returns Uint8Array
. You need first to convert it to a Buffer, then stringify, and then parse JSON.
Replace this line in your code:
return response.Payload;
With this line:
return JSON.parse(Buffer.from(response.Payload).toString())