User Agents

Web browsers and everyday web tech, explained

Deploy Playwright & Chrome on AWS Lambda: Zip & Container

· 5 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

This guide walks you through deploying Playwright with Chromium on AWS Lambda using both a ZIP package and a container image. By the end, you'll have a working Lambda function that can run browser automation tasks like scraping, PDF generation, or screenshot capture. You'll need an AWS account, Node.js 18+ installed locally, Docker (for the container method), and basic familiarity with the AWS CLI and Lambda console.

What You'll Need

  • AWS account with permissions to create Lambda functions and IAM roles
  • AWS CLI installed and configured (optional but helpful)
  • Node.js 18 or newer (for local testing and packaging)
  • Docker installed (only for the container method)
  • Playwright installed locally via npm

Step-by-Step

Method 1: ZIP Package

  1. Create a new Node.js project: Run npm init -y in an empty directory.
  2. Install Playwright: Run npm install playwright-core (not full Playwright, to keep size down).
  3. Install Chromium browser: Run npx playwright install chromium to download the browser locally.
  4. Write your Lambda function: Create index.js with a handler that launches Chromium, performs your task, and closes the browser. Use chromium.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] }).
  5. Package dependencies: Run npm install --production to ensure only production dependencies are included.
  6. Copy the Chromium executable: Find the browser path (usually in node_modules/playwright-core/.local-browsers/) and copy it into your project folder, or set PLAYWRIGHT_BROWSERS_PATH=0 to keep it relative.
  7. Create the ZIP: Zip all files including index.js, node_modules, and the browser folder. Do not include the outer project folder.
  8. Upload to Lambda: In the AWS Lambda console, create a new function, choose "Upload from .zip file", and select your ZIP.
  9. Set environment variables: Add PLAYWRIGHT_BROWSERS_PATH=/var/task/.local-browsers (or wherever you placed the browser) and CHROMIUM_EXECUTABLE_PATH to point to the executable.
  10. Increase memory and timeout: Set memory to at least 1024 MB and timeout to 60 seconds or more, as browser startup takes time.

Method 2: Container Image

  1. Create a Dockerfile: Use a base image like public.ecr.aws/lambda/nodejs:18.
  2. Install system dependencies: Run dnf install -y atk cups-libs libXcomposite libXcursor libXdamage libXext libXi libXrandr libXrender libXtst pango alsa-lib (for Amazon Linux 2) or the equivalent for Debian-based images.
  3. Install Playwright: Copy your package.json, run npm install playwright-core, then npx playwright install chromium --with-deps.
  4. Copy your function code: Add COPY index.js . to the image.
  5. Set the Lambda runtime: Add CMD ["index.handler"] to your Dockerfile.
  6. Build the image: Run docker build -t playwright-lambda ..
  7. Push to Amazon ECR: Create an ECR repository, authenticate, and push your image using the AWS CLI.
  8. Create Lambda from container: In the Lambda console, choose "Container image", select your ECR image, and set memory/timeout as above.

Common Mistakes

  • Forgetting to include the Chromium browser in the ZIP or container – the function fails with "Executable doesn't exist" errors.
  • Using the full Playwright package instead of playwright-core, which bloats size and slows cold starts.
  • Not setting the --no-sandbox flag – Lambda runs as root, and Chromium's sandbox fails in that environment.
  • Setting memory too low (under 512 MB) – Chromium needs at least 256 MB just to start, but 1 GB is safer.

Tips

  • Use playwright-core and manage the browser binary separately to keep your package lean.
  • Set PLAYWRIGHT_BROWSERS_PATH=0 in your code to make Playwright look for browsers in the same directory as the module.
  • For the ZIP method, test locally with the same environment variables to catch missing paths.
  • For the container method, use a minimal base image like amazon/aws-lambda-nodejs:18 and install only required system libraries.
  • Consider using Lambda layers for the browser binary to reduce deployment size and speed up updates.

Troubleshooting

  • Error: "Executable doesn't exist at ..." – Check the browser path. Ensure the browser is present in the deployment package and that PLAYWRIGHT_BROWSERS_PATH is set correctly.
  • Error: "Failed to launch browser: missing shared libraries" – For containers, add all required system dependencies (like libX11, libXcomposite). For ZIP, ensure you're using a compatible Amazon Linux environment.
  • Cold start timeout – Increase the Lambda timeout to at least 60 seconds. If it still times out, reduce memory or optimize your code.
  • Function runs but no output – Check CloudWatch logs for errors. Ensure you're closing the browser properly in your handler.
  • ZIP size exceeds Lambda limit – Lambda has a 250 MB unzipped limit. Compress the browser binary or use a container image which allows up to 10 GB.

Frequently Asked Questions

Can I use Playwright with Python on Lambda?

Yes, the same concepts apply. Install playwright via pip, run playwright install chromium, and package the browser. For containers, use a Python base image and install dependencies.

How do I keep the Lambda package under the size limit?

Use playwright-core and exclude the browser from the ZIP by storing it in an S3 bucket or a Lambda layer. For containers, you have more room (up to 10 GB).

Why does my function work locally but fail on Lambda?

Lambda runs on Amazon Linux, which may lack system libraries. Also, the browser path must be absolute and accessible. Use the same environment variables and test with the same OS if possible.

Can I use headless mode?

Yes, Playwright's default is headless. You don't need a display server. Just launch with chromium.launch() and it works.

How do I handle concurrent invocations?

Each Lambda invocation gets its own instance, so you can launch a browser per invocation. Be mindful of memory and cold start times. For high concurrency, consider using a persistent browser via a separate service.

Is there a way to reduce cold start time?

Use a container image with a warm start, increase memory (which also increases CPU), and keep the browser binary small. Also, avoid loading unnecessary modules in your handler.