User Agents

Web browsers and everyday web tech, explained

Deploy Puppeteer and Chrome on AWS Lambda Guide

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Deploying Puppeteer and Chrome on AWS Lambda allows you to automate browser tasks—such as PDF generation, web scraping, and automated testing—within a serverless environment. Because AWS Lambda functions run in a stripped-down Linux environment, they lack the necessary shared libraries and the Chrome binary required to run a headless browser. To solve this, you must use a specialized version of Chromium designed for Lambda and package it either as a Lambda Layer or within a Docker container.

What You'll Need

  • An AWS Account with IAM permissions to create Lambda functions and Layers.
  • Node.js installed locally for development.
  • The npm package manager.
  • A code editor (such as VS Code).
  • (Optional) Docker installed if you prefer container-based deployment over Layers.

Step-by-Step

  1. Initialize Your Project: Create a new directory for your project and initialize it with npm init -y. Install the puppeteer-core package instead of the standard puppeteer package. puppeteer-core is a lightweight version that does not automatically download a bundled browser, which is essential because the bundled browser will not run on Lambda's architecture.
  2. Integrate chromium-lambda: Install @sparticuz/chromium. This package provides a pre-compiled Chromium binary specifically optimized for the AWS Lambda environment, including the necessary shared libraries that are missing from the standard Lambda runtime.
  3. Write the Lambda Handler: Create an index.js file. In your code, import puppeteer from puppeteer-core and chromium from @sparticuz/chromium. When launching the browser, you must pass specific arguments to ensure it runs in a headless, sandbox-less environment. Use puppeteer.launch({ args: chromium.args, executablePath: await chromium.executablePath() }).
  4. Configure Memory and Timeout: Headless Chrome is resource-intensive. Go to the AWS Lambda Console, navigate to Configuration > General configuration, and increase the memory to at least 1600 MB. Increase the timeout to 30 seconds or more, as browser initialization and page loading can be slow.
  5. Package and Deploy via Layers (Option A): If your project is small, create a Lambda Layer containing the @sparticuz/chromium binary. Zip the node_modules folder and upload it as a layer. This keeps your main function code small and easy to edit in the AWS console.
  6. Package and Deploy via Docker (Option B): For larger projects or more stability, create a Dockerfile. Use the official AWS Lambda Node.js base image. Copy your code and package.json into the image, run npm install, and push the image to Amazon Elastic Container Registry (ECR). Deploy the Lambda function using this container image.
  7. Test the Execution: Create a test event in the Lambda console. Trigger the function and check the logs in CloudWatch to ensure the browser launched and the page was navigated successfully.

Common Mistakes

  • Using standard Puppeteer: Installing the full puppeteer package often leads to deployment failures because it attempts to download a version of Chrome that is incompatible with the Lambda Linux environment.
  • Insufficient Memory: Running Chrome with the default 128MB or 512MB of RAM usually results in Crash or Out of Memory errors. Always allocate at least 1.6GB.
  • Forgetting the Sandbox Flag: Chrome's sandbox requires privileges that Lambda does not provide. Failing to include --no-sandbox in the launch arguments will prevent the browser from starting.
  • Ignoring the /tmp Directory: Lambda's filesystem is read-only except for the /tmp directory. If your Puppeteer script needs to save files or download assets, ensure the path points to /tmp/.

Tips

  • Use a Singleton Pattern: To improve performance across warm starts, define the browser variable outside the handler function. Check if the browser is already open before launching a new instance to reduce latency.
  • Optimize Page Loading: Use page.setRequestInterception(true) to block unnecessary resources like images, CSS, and fonts if you only need the HTML content. This significantly speeds up execution and reduces memory usage.
  • Use AWS ECR: Container images allow for up to 10GB of size, removing the strict 250MB unzipped limit of standard Lambda zip deployments.

Troubleshooting

If your function fails, check the following areas:

  • "Executable not found" errors: This usually means the path provided by chromium.executablePath() is incorrect or the binary was not properly packaged in the layer/image. Verify that @sparticuz/chromium is correctly installed.
  • Timeout errors: If the function times out, check if the website you are scraping has bot detection that is hanging the request. Try adding a custom User-Agent string to mimic a real browser.
  • Shared Library errors (.so files): This occurs when the Chromium binary is incompatible with the Lambda runtime version (e.g., using a Node 14 binary on a Node 18 runtime). Ensure your Chromium package version matches your Node.js runtime version.

Frequently Asked Questions

Does Puppeteer work with the free tier of AWS Lambda?

Yes, but be mindful of the memory requirements. Since Puppeteer requires higher memory settings, you will consume your free tier memory-seconds faster than with a simple API function.

Can I run a headed browser on Lambda?

No. AWS Lambda is a headless environment with no display server (X11). You must run Chrome in headless: true mode.

Which is better: Lambda Layers or Docker containers?

Docker containers are generally more reliable for Puppeteer because they avoid the 250MB deployment limit and allow you to control the OS dependencies more precisely.

How do I handle CAPTCHAs in Lambda?

Puppeteer on Lambda cannot solve CAPTCHAs natively. You will need to integrate a third-party CAPTCHA solving API via HTTP requests within your script.

Is there a limit to how many pages I can open?

Yes, the limit is governed by the memory you allocate. Opening too many tabs/pages simultaneously will lead to an Out of Memory crash.