Use playwright-aws-lambda in an AWS Lambda handler
mainThe package supports Node.js runtimes nodejs10.x through nodejs20.x. To use it, import playwright-aws-lambda and use the launchChromium() method.
Note: Currently, only Chromium is supported.
Always ensure you close the browser instance in a finally block to prevent resource leaks in the Lambda environment.
const playwright = require('playwright-aws-lambda');
exports.handler = async (event, context) => {
let browser = null;
try {
browser = await playwright.launchChromium();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(event.url || 'https://example.com');
console.log('Page title: ', await page.title());
} catch (error) {
throw error;
} finally {
if (browser) {
await browser.close();
}
}
};