Open LuisAlberto22 opened 2 years ago
Same issue on AWS Lambda on both node.js versions 12.x and 16.x. Whenever I try to generate a pdf, I get the following in the cloudwatch logs:
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:241:20)
at Interface.<anonymous> (/opt/nodejs/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:231:68)
at Interface.emit (events.js:326:22)
at Interface.close (readline.js:416:8)
at Socket.onend (readline.js:194:10)
at Socket.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1241:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Is this an issue with the package itself?
For me it was because the default memory allocated to the lambda was too low. I bumped it to 1024 MB and it works perfectly now.
I tried bumping the memory upto 1024 MB. But the issue still persists.
@lakshyajit165 hi, I've already fix my error, could you upload your code to see the problem?
@LuisAlberto22 sure. Here is my code in the index.js file of a Lambda function. The function is invoked through an API gateway whose content-type in the success case is 'application/pdf' as I intend to return a pdf in the response.
const chromium = require('chrome-aws-lambda');
exports.handler = async (event) => {
// TODO implement
let browser = null;
let result = null;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true,
});
let page = await browser.newPage();
await page.goto(event.url || 'https://example.com');
result = await page.title();
const response = {
statusCode: 200,
headers: {'Content-type' : 'application/pdf'},
body: result.toString('base64'),
isBase64Encoded : true,
};
return response;
} catch (err) {
console.error(err);
const errorResponse = {
message: "An error occurred while generating PDF. Please try again!"
};
const response = {
statusCode: 500,
headers: {'Content-type' : 'application/json'},
body: JSON.stringify(errorResponse)
};
return response;
} finally {
if (browser !== null) {
await browser.close();
}
}
};
@lakshyajit165 wich node version your lambda is runing? this configuration works for me my lambda runs with x14 and my layer too.
@LuisAlberto22 I was previously using 512 MB of ephemeral storage. I changed it to 1024 and tested, but still facing the same issue. Rest all config like node.js runtime env, memory etc are same :(
And just for your reference. This is what my package.json looks like (which I use to zip and upload to the layer for lambda)
{
"name": "nodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chrome-aws-lambda": "^10.1.0",
"puppeteer-core": "^13.1.0"
}
}
@lakshyajit165 try to install puppeteer-core 10.1.0 or 10.4.0 you have the puppeteer version 13.1.0 and chrome-aws-lambda version 10.1.0 the difference between these could be the problem
@LuisAlberto22 same issue with both the versions of puppeteer-core you mentioned. :/
Can you see how much memory your app is using??
Ok...not sure how/where to check that though...
you can check it out on your lambda Console https://yourRegion.console.aws.amazon.com/lambda/ then run your function by clicking here
run your lambda, and see your Execution result
Scroll down and you see the summary with the maxium memory used by the lambda
@LuisAlberto22 This is a snippet from the cloudwatch logs of the time when the request hit.
REPORT RequestId: 62da5a5f-498a-43b9-87fb-4d7179def238 Duration: 3169.01 ms Billed Duration: 3000 ms Memory Size: 1024 MB Max Memory Used: 376 MB Init Duration: 193.29 ms
As per this the memory used is 376 MB.
Ok, this is the last thing I think could work, you have to create a layer Chrome on layer, this works with Node.js 16x runtime
@lakshyajit165 that error is fixed by adding executablePath: await chromium.executablePath
to the browser config
@LuisAlberto22 that is already added. Just need a clarification, when I added Chrome on layer, do I need to change my other layer in which "chrome-aws-lambda" dependency was present or I specifically need "@sparticuz/chrome-aws-lambda" in my package.json as well and bundle it and apply it again?
no, you don't need to change your layer, just add the new layer that is on the link, change the requiere for const const chromium = require('@sparticuz/chrome-aws-lambda');
and change the runtime for x16
Got it. Did all that...still same issue...may be I have to look at a different method to generate the pdf afterall. I only went after puppeteer 'cause it's easy to use. Thanks for your swift responses :)
@lakshyajit165 I'm also facing the same issue , did you found any resolution or any alternative to generate pdf using aws lambda .?
@saibhargav022 not yet, the only problem is puppeteer is actually a heavy package. And the implementations for that which are supposed to be compatible with lambda don't work so well(based on my findings)
@lakshyajit165 can you provide me a snapshot of your aws lambda environment bar
@saibhargav022 sorry but that env is no longer active with me. I brought it down to avoid incurring accidental charges.
@lakshyajit165 where should the package.json file is supposed to be , is it inside the environment trajectory or in the node modules trajectory .?
@lakshyajit165 did you find any solution for this issue.
No. Actually I realized, serverless is kind of useless
Hi, I followed every step for Aws Lambda, but I get this Bug Failed to launch the browser process!\n\n\nTROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md\n",
this is my packet
"dependencies": { "@sparticuz/chrome-aws-lambda": "^17.1.1", "puppeteer-core": "^17.1.3" }