Reputation: 479
I have a lambda function,I get buffer from external api which is approximate 2.5 MB size but when I return same buffer in JSON object it becomes more than 7.5 MB, which is grater than lambda response size (6MB)limit,I am not sure how it is happening. Here is my code
module.exports.handler = (event, context, callback) => {
const buffer = getAnswer();
// This is approx 2.1 MB buffer size
console.log(`Buffer size ${buffer.byteLength}`);
const response = {
headers: {
'Content-Type': 'application/json',
},
isBase64Encoded: false,
statusCode: 200,
statusDescription: '200 OK',
body: JSON.stringify(buffer),
};
// Here size becomes more than 7.5 MB ,I am not sure how it happens
console.log('This becomes 7.5 MB size', Buffer.byteLength(JSON.stringify(response)), 'bytes');
context.succeed(response);// Gives error because it exceeds more than 6MB lambda response limit
};
Upvotes: 0
Views: 1197
Reputation: 4596
Inspect the result of JSON.stringify(buffer)
and the reason will be obvious.
When you JSON.stringify a buffer the buffer is first converted to an object like this
{
"type": "Buffer",
"data": [byte0, byte1, byte2, byte3, .... ]
}
where byteX is the integer value of the byte.
If the buffer is text/JSON set the body to buffer.toString()
.
If you need to return binary consider adding a binary type to the rest api in ApiGateway and returning a base64 encoded version of the data.
Upvotes: 2