I am using Puppeteer/Chromium on AWS Lambda. In my lambda handler, I am launching/closing a Chromium instance every time the lambda is called.
Here is a simplified example baed on @sparticuz/chromium
documentation:
const chromium = require("@sparticuz/chromium");
const puppeteer = require("puppeteer-core");
export const handler = async (event, context) => {
let result = null;
let browser = null;
try {
browser = await puppeteer.launch({
args: [
...chromium.args,
'--disable-gpu',
'--single-process',
],
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');
return await page.title();
} catch (error) {
console.log('Error: ', error);
} finally {
if (browser !== null) {
await browser.close();
}
}
};
Launching Chromium takes some significant time and I’d like to reduce latency in case of warm/hot start. AWS documentation on Optimizing static initialization states that global variables retain their value between invocations in the same execution environment.
Can I move the browser to a global variable? If so, what are the best practices so the lambda instance is not stuck if the browser crash? I’d also like to prevent the exhausting memory (e.g. loading of an unbounded amount of pages) if the handler to not close gracefully.