playwright-aws-lambda

repository·main·Indexed 19 days ago

https://github.com/jupiterone/playwright-aws-lambda

Support for running Playwright (specifically Chromium) on AWS Lambda and Google Cloud Functions. Provides utilities for launching Chromium in constrained environments, including launchChromium(), getChromiumArgs(), and loadFont() for custom character support. Supports Node.js runtimes nodejs10.x through nodejs20.x.

Tokens
1.8K
Snippets
9
Records
9
Agent score
65%

What's inside playwright-aws-lambda

  1. Use playwright-aws-lambda in an AWS Lambda handler

    main

    The 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();
        }
      }
    };
  2. Load additional fonts for custom character support

    main

    If your browser automation requires custom font support (for example, to render emojis correctly), use the loadFont(url) function. This must be called before you launch the browser instance.

    await loadFont(
      'https://raw.githack.com/googlei18n/noto-emoji/master/fonts/NotoColorEmoji.ttf'
    );
  3. API Reference for playwright-aws-lambda

    main

    The following methods are available on the playwright-aws-lambda module:

    | Method / Property | Returns | Description |
    | ----------------- | ------------------------------------- | ------------------------------------- |
    | `launchChromium`  | `{!Promise<playwright.ChromiumBrowser>}` | Launches the Chromium browser. |
    | `loadFont(url)`   | `{Promise<void>}`                        | Downloads and activates a custom font |
  4. Get recommended Chromium arguments with `getChromiumArgs`

    main

    The getChromiumArgs function returns a pre-configured list of Chromium flags optimized for running in constrained environments like AWS Lambda. These flags disable unnecessary features (like background networking, sync, and extensions) to reduce resource usage and prevent common errors in headless/Lambda environments.

    If headless is set to true, it includes the --single-process flag. If false, it includes --start-maximized.

    import { getChromiumArgs } from 'playwright-aws-lambda/src/chromium';
    
    const args = getChromiumArgs(true);
    console.log(args);
  5. Environment variables configured by getEnvironmentVariables

    main

    When running in an AWS Lambda environment, the getEnvironmentVariables function automatically configures specific environment variables required for Playwright to function correctly (specifically for font rendering and library loading).

    If you are calling this function to prepare your Lambda environment, it manages the following:

    • FONTCONFIG_PATH: Points to the directory containing font configurations. If a custom font directory (/tmp/fonts) exists, it creates a fonts.conf file there. Otherwise, it defaults to /tmp/aws.
    • LD_LIBRARY_PATH: Ensures the Playwright libraries located in /tmp/aws/lib are included in the system's library search path. If LD_LIBRARY_PATH is already set, the function prepends the AWS library directory to the existing path to avoid overwriting other necessary paths.

    Note: If the code is not running in an AWS Lambda runtime environment, the function returns an empty object {}.

    import getEnvironmentVariables from './util/getEnvironmentVariables';
    
    // In a Lambda handler context:
    const envVars = await getEnvironmentVariables();
    // envVars will contain FONTCONFIG_PATH and LD_LIBRARY_PATH
  6. Launch Chromium with `launchChromium`

    main

    Use launchChromium to start a Chromium instance optimized for AWS Lambda environments. It automatically handles Chromium arguments, executable path resolution (including inflating binaries from the package), and environment variable merging.

    By default, it respects the headless mode setting of your environment. You can pass standard Playwright LaunchOptions to customize the launch behavior.

    import { launchChromium } from 'playwright-aws-lambda/src/chromium';
    
    // Launch with default optimized settings
    const browser = await launchChromium();
    
    // Launch with custom options
    const browser = await launchChromium({
      headless: true,
      env: {
        MY_CUSTOM_VAR: 'value'
      }
    });
  7. Use playwright-aws-lambda via the chromium entrypoint

    main

    The playwright-aws-lambda package exports its primary functionality through the chromium module. To use the package, import from the main entrypoint, which re-exports the chromium implementation. This allows you to run Playwright with Chromium in an AWS Lambda environment.

    import { chromium } from 'playwright-aws-lambda';
    
    // Use chromium to launch a browser in Lambda
    const browser = await chromium.launch();
  8. Load custom fonts with `loadFont`

    main

    The loadFont function allows you to download and register a font from a URL so it can be used by Chromium.

    It downloads the file from the provided URL and saves it to the directory specified by the AWS_FONT_DIR environment variable. If the font already exists in that directory, the function resolves immediately without re-downloading.

    import { loadFont } from 'playwright-aws-lambda/src/chromium';
    
    // Download and cache a font from a URL
    await loadFont('https://example.com/fonts/roboto.ttf');