puppeteer screenshot running on aws lambda crashes when encountering large image on site
See original GitHub issueSteps to reproduce
Tell us about your environment:
- Puppeteer version: 1.12.2
- Platform / OS version: aws lambda
- URLs (if applicable):
- Node.js version: 8.10
- Lambda memory size: 3gb (with 30 sec timeout)
What steps will reproduce the problem?
const chromium = require('chrome-aws-lambda');
const puppeteer = require('puppeteer-core');
const SCREENSHOT_WIDTH = 1200;
const SCREENSHOT_HEIGHT = 808;
exports.handler = async (event, context, callback) => {
let url = '';
let tokenHash = '';
if (event.queryStringParameters) {
if (event.queryStringParameters.url) {
url = event.queryStringParameters.url;
}
if (event.queryStringParameters.t) {
tokenHash = event.queryStringParameters.t;
}
}
let browser = null;
let rawScreenshot = null;
try {
browser = await puppeteer.launch({
args: chromium.args,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
const page = await browser.newPage();
await page.setViewport({ width: SCREENSHOT_WIDTH, height: SCREENSHOT_HEIGHT });
await page.goto(url, {});
rawScreenshot = await page.screenshot({});
await page.close();
} catch (e) {
console.log('token hash: ' + tokenHash);
console.log(e);
callback(null, {
statusCode: 500,
body: e.message,
headers: {
'Content-Type': 'text/plain',
},
});
} finally {
if (browser !== null) {
await browser.close();
}
}
callback(null, {
statusCode: 200,
isBase64Encoded: true,
body: rawScreenshot.toString('base64'),
headers: {
'Content-Type': 'image/png',
},
});
};
- I deploy my function to AWS lambda and I invoke it with the url param
- sample site I’m using https://bluelitephotography.myportfolio.com
What is the expected result? A screenshot being returned
What happens instead? I consistently get the following error:
Protocol error (Page.captureScreenshot): Target closed.
Notice the 4.5 mb image being loaded on that page. The same code return screenshot successfully with most other websites.
Issue Analytics
- State:
- Created 5 years ago
- Comments:15 (1 by maintainers)
Top Results From Across the Web
Scaling Browser Automation with Puppeteer on AWS Lambda ...
This blog will show how to run Puppeteer and Chrome in a Lambda container function. In this example, multiple instances of Puppeteer will ......
Read more >Troubleshooting - Puppeteer
UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process! This means that the browser was downloaded but failed to be extracted correctly.
Read more >Puppeteer - Protocol error (Page.navigate): Target closed
The Target closed exception is thrown when you are trying to run a function, but the target (tab) was already closed.
Read more >Serverless Handbook - Amazon AWS
Take screenshots . ... telling the Serverless framework that we want to use AWS, run ... AWS retries every lambda invocation76 , if...
Read more >Untitled
#scooter Metamorphic rocks examples pictures. Use tablet as remote for pc, Jugendjury berlin kreuzberg, 05 fiesta bowl, Angsa putih lirik dan chord, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To add more information: We’ve been running Puppeteer on Lambda (Node 10.x runtime) using
chrome-aws-lambda
with the default settings (and others while debugging, nothing worked).When rendering HTML with an image with the following information, the browser process dies.
With
dumpio: true
, we get:What’s odd is the Lambda is configured to the maximum 3008MB. The ~155MB mentioned above is significantly less than that, and I don’t believe our Node service is using up the rest of it.
Any ideas of limits that may be not allowing Chromium to use all of the Lambda memory?
@amazd Tab crashes, most likely running out of memory. I don’t think we can do much about it on the puppeteer side; running chrome in a constrained environment is an art by itself.
I’d start with https://github.com/alixaxel/chrome-aws-lambda - a well-maintained and actually used by folks in production. There are also many more articles on the Web that might be helpful.
Hope this helps.