### Install playwright-aws-lambda Source: https://github.com/jupiterone/playwright-aws-lambda/blob/main/README.md Install the necessary packages for playwright-aws-lambda. ```shell npm install playwright-core playwright-aws-lambda --save ``` -------------------------------- ### Get Recommended Chromium Launch Arguments Source: https://context7.com/jupiterone/playwright-aws-lambda/llms.txt Use `getChromiumArgs` to obtain an array of Chromium CLI flags optimized for serverless environments. These flags can be inspected or extended with custom arguments. ```javascript const { getChromiumArgs } = require('playwright-aws-lambda'); const { chromium } = require('playwright-core'); async function customLaunch() { const headless = true; const args = getChromiumArgs(headless); // Inspect the default flags console.log('Default Chromium args:', args); // [ // '--autoplay-policy=user-gesture-required', // '--disable-dev-shm-usage', // '--no-sandbox', // '--no-zygote', // '--use-gl=swiftshader', // '--single-process', // added because headless=true // ... (30+ flags) // ] // Extend with your own flags const customArgs = [ ...args, '--proxy-server=http://my-proxy:3128', '--disable-web-security', ]; const browser = await chromium.launch({ args: customArgs, headless, }); const page = await (await browser.newContext()).newPage(); await page.goto('https://example.com'); await browser.close(); } ``` -------------------------------- ### Launch Chromium in Lambda Source: https://context7.com/jupiterone/playwright-aws-lambda/llms.txt Launch a headless Chromium browser instance within an AWS Lambda function. This example demonstrates basic navigation, capturing a screenshot, and handling potential errors. The browser is automatically closed in the `finally` block. ```javascript const playwright = require('playwright-aws-lambda'); exports.handler = async (event) => { let browser = null; try { // Launch with default options (headless auto-detected from environment) browser = await playwright.launchChromium(); const context = await browser.newContext({ userAgent: 'MyBot/1.0', viewport: { width: 1280, height: 800 }, }); const page = await context.newPage(); // Navigate and capture a screenshot await page.goto(event.url || 'https://example.com', { waitUntil: 'networkidle', }); const title = await page.title(); const screenshot = await page.screenshot({ type: 'png', fullPage: true }); console.log('Page title:', title); // screenshot is a Buffer containing PNG data return { statusCode: 200, headers: { 'Content-Type': 'image/png' }, body: screenshot.toString('base64'), isBase64Encoded: true, }; } catch (error) { console.error('Browser automation failed:', error); throw error; } finally { if (browser) { await browser.close(); } } }; ``` -------------------------------- ### Load Custom Fonts Before Launching Browser Source: https://context7.com/jupiterone/playwright-aws-lambda/llms.txt Call `loadFont` before `launchChromium` to ensure custom fonts are available when the browser starts. This downloads the font and configures Chromium to discover it. ```javascript const playwright = require('playwright-aws-lambda'); exports.handler = async (event) => { let browser = null; try { // Load emoji font before launching the browser await playwright.loadFont( 'https://raw.githack.com/googlei18n/noto-emoji/master/fonts/NotoColorEmoji.ttf' ); // Load an additional CJK font for Chinese/Japanese/Korean rendering await playwright.loadFont( 'https://fonts.gstatic.com/s/notosansjp/v42/-F6jfjtqLzI2JPCgQBnw7HFQaioq1H1hj-sNFQ.woff2' ); browser = await playwright.launchChromium(); const context = await browser.newContext(); const page = await context.newPage(); // Render emoji-heavy content correctly await page.setContent(`

Playwright 🎭 on Lambda πŸš€

γ“γ‚“γ«γ‘γ―δΈ–η•Œ

`); const screenshot = await page.screenshot({ type: 'png' }); // screenshot now contains correctly rendered emoji and CJK characters return { statusCode: 200, body: screenshot.toString('base64'), isBase64Encoded: true, }; } finally { if (browser) await browser.close(); } }; ``` -------------------------------- ### loadFont(url) Source: https://context7.com/jupiterone/playwright-aws-lambda/llms.txt Downloads and registers a custom font from a given HTTPS URL. This function must be called before `launchChromium` to ensure the font is available when the browser starts. It handles caching by skipping the download if the font file already exists. ```APIDOC ## `loadFont(url)` β€” Download and register a custom font Downloads a font file from the given HTTPS URL to `/tmp/fonts/` and writes a `fonts.conf` so that Chromium can discover it. If the font file already exists on disk, the download is skipped. Must be called **before** `launchChromium` so that the font is available when the browser starts. Returns `Promise`. ``` -------------------------------- ### AWS Lambda Handler with Playwright Source: https://github.com/jupiterone/playwright-aws-lambda/blob/main/README.md Example of an AWS Lambda handler function that uses playwright-aws-lambda to launch Chromium, navigate to a URL, and log the page title. Ensure the browser is closed in the finally block. ```javascript 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(); } } }; ``` -------------------------------- ### getChromiumArgs(headless) Source: https://context7.com/jupiterone/playwright-aws-lambda/llms.txt Provides an array of recommended Chromium CLI flag strings optimized for serverless environments. It adjusts flags based on the `headless` parameter, adding `--single-process` for headless mode to reduce memory usage and `--start-maximized` for non-headless mode. These flags are useful for inspecting, extending, or passing directly to a custom Playwright launch configuration. ```APIDOC ## `getChromiumArgs(headless)` β€” Get recommended Chromium launch flags Returns an array of Chromium CLI flag strings tuned for the serverless environment. In headless mode (`true`), adds `--single-process` to reduce memory usage; in non-headless mode (`false`), adds `--start-maximized`. Useful when you need to inspect, extend, or pass the flags directly to a custom Playwright launch call. ``` -------------------------------- ### launchChromium Source: https://context7.com/jupiterone/playwright-aws-lambda/llms.txt Launches a Chromium browser instance within a serverless environment. It extracts the bundled Chromium binary, sets up necessary environment variables, and launches Chromium using playwright-core. It accepts optional launch options to override defaults and returns a Promise resolving to a ChromiumBrowser instance. ```APIDOC ## launchChromium(launchOptions?) ### Description Extracts the bundled Chromium binary to `/tmp` (if not already present), sets up `LD_LIBRARY_PATH` and `FONTCONFIG_PATH` for the Lambda environment, and launches Chromium via `playwright-core`. Accepts an optional partial `LaunchOptions` object (same interface as `playwright-core`) to override any defaults such as `slowMo`, `timeout`, or additional `args`. Returns a `Promise`. ### Parameters #### Request Body - **launchOptions** (object) - Optional - An object containing Playwright `LaunchOptions` to override defaults. - **timeout** (number) - Optional - Timeout for the launch operation. - **slowMo** (number) - Optional - Slows down Playwright operations by the specified amount. - **args** (array) - Optional - Additional Chromium command-line arguments. ### Request Example ```javascript const playwright = require('playwright-aws-lambda'); exports.handler = async (event) => { let browser = null; try { // Launch with default options (headless auto-detected from environment) browser = await playwright.launchChromium(); const context = await browser.newContext({ userAgent: 'MyBot/1.0', viewport: { width: 1280, height: 800 }, }); const page = await context.newPage(); // Navigate and capture a screenshot await page.goto(event.url || 'https://example.com', { waitUntil: 'networkidle', }); const title = await page.title(); const screenshot = await page.screenshot({ type: 'png', fullPage: true }); console.log('Page title:', title); // screenshot is a Buffer containing PNG data return { statusCode: 200, headers: { 'Content-Type': 'image/png' }, body: screenshot.toString('base64'), isBase64Encoded: true, }; } catch (error) { console.error('Browser automation failed:', error); throw error; } finally { if (browser) { await browser.close(); } } }; // Override launch options β€” e.g. set a longer timeout and extra flags exports.handlerWithOptions = async () => { const browser = await playwright.launchChromium({ timeout: 60000, slowMo: 100, args: ['--window-size=1920,1080'], }); const page = await (await browser.newContext()).newPage(); await page.goto('https://example.com'); await browser.close(); }; ``` ### Response #### Success Response (200) - **ChromiumBrowser** (object) - A Playwright Chromium browser instance. ``` -------------------------------- ### launchChromium Source: https://github.com/jupiterone/playwright-aws-lambda/blob/main/README.md Launches the Chromium browser instance. This is a core function for interacting with web pages in a serverless environment. ```APIDOC ## launchChromium ### Description Launches the Chromium browser. ### Method `launchChromium()` ### Returns `{!Promise}` - A promise that resolves to a Chromium browser instance. ``` -------------------------------- ### Local Development with IS_LOCAL Environment Variable Source: https://context7.com/jupiterone/playwright-aws-lambda/llms.txt Set the `IS_LOCAL` environment variable to `true` to disable headless mode and launch a visible Chromium window for local debugging. This allows using the same handler code on your machine. ```javascript // Environment variables recognized by playwright-aws-lambda: // // IS_LOCAL (any value) // β†’ Disables headless mode so Chromium launches a visible window. // Useful for local development and debugging. // // AWS_LAMBDA_FUNCTION_NAME / FUNCTION_NAME / FUNCTION_TARGET // β†’ Presence of any of these signals a serverless runtime, // enabling headless mode and Lambda-specific setup. // // AWS_EXECUTION_ENV // β†’ Must equal one of: // 'AWS_Lambda_nodejs10.x' | 'nodejs12.x' | 'nodejs14.x' | // 'nodejs16.x' | 'nodejs18.x' | 'nodejs20.x' // to trigger AWS-specific binary extraction (aws.tar.br). // // FONTCONFIG_PATH // β†’ If not set and /tmp/fonts exists, automatically configured // to point at /tmp/fonts with a generated fonts.conf. // // LD_LIBRARY_PATH // β†’ /tmp/aws/lib is prepended automatically for Lambda runtimes // so that native shared libraries extracted from aws.tar.br // are found by the Chromium process. // --- Local development example --- // Set IS_LOCAL to run the same handler code on your machine: process.env.IS_LOCAL = 'true'; const playwright = require('playwright-aws-lambda'); (async () => { // Launches a visible (non-headless) Chromium window locally const browser = await playwright.launchChromium(); const page = await (await browser.newContext()).newPage(); await page.goto('https://example.com'); console.log(await page.title()); // => 'Example Domain' await browser.close(); })(); ``` -------------------------------- ### Launch Chromium with Custom Options Source: https://context7.com/jupiterone/playwright-aws-lambda/llms.txt Launch Chromium with custom launch options, such as increased timeout, slow motion, and specific arguments. This allows for fine-tuning the browser's behavior for different scenarios. ```javascript // Override launch options β€” e.g. set a longer timeout and extra flags exports.handlerWithOptions = async () => { const browser = await playwright.launchChromium({ timeout: 60000, slowMo: 100, args: ['--window-size=1920,1080'], }); const page = await (await browser.newContext()).newPage(); await page.goto('https://example.com'); await browser.close(); }; ``` -------------------------------- ### loadFont Source: https://github.com/jupiterone/playwright-aws-lambda/blob/main/README.md Downloads and activates a custom font. This is useful for ensuring specific fonts, like emoji fonts, are available in the browser context. ```APIDOC ## loadFont ### Description Downloads and activates a custom font. ### Method `loadFont(url: string)` ### Parameters #### Path Parameters - **url** (string) - Required - The URL of the font file to download. ### Returns `{Promise}` - A promise that resolves when the font has been loaded and activated. ``` -------------------------------- ### Load Custom Font for Playwright Source: https://github.com/jupiterone/playwright-aws-lambda/blob/main/README.md Load a custom font, such as NotoColorEmoji, before launching the browser to support specific characters like emojicons. This function should be called prior to `launchChromium`. ```javascript await loadFont( 'https://raw.githack.com/googlei18n/noto-emoji/master/fonts/NotoColorEmoji.ttf' ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.