Deploy Playwright & Chrome on AWS Lambda: Zip & Container
In this article
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
- Create a new Node.js project: Run
npm init -yin an empty directory. - Install Playwright: Run
npm install playwright-core(not full Playwright, to keep size down). - Install Chromium browser: Run
npx playwright install chromiumto download the browser locally. - Write your Lambda function: Create
index.jswith a handler that launches Chromium, performs your task, and closes the browser. Usechromium.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] }). - Package dependencies: Run
npm install --productionto ensure only production dependencies are included. - Copy the Chromium executable: Find the browser path (usually in
node_modules/playwright-core/.local-browsers/) and copy it into your project folder, or setPLAYWRIGHT_BROWSERS_PATH=0to keep it relative. - Create the ZIP: Zip all files including
index.js,node_modules, and the browser folder. Do not include the outer project folder. - Upload to Lambda: In the AWS Lambda console, create a new function, choose "Upload from .zip file", and select your ZIP.
- Set environment variables: Add
PLAYWRIGHT_BROWSERS_PATH=/var/task/.local-browsers(or wherever you placed the browser) andCHROMIUM_EXECUTABLE_PATHto point to the executable. - 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
- Create a Dockerfile: Use a base image like
public.ecr.aws/lambda/nodejs:18. - 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. - Install Playwright: Copy your
package.json, runnpm install playwright-core, thennpx playwright install chromium --with-deps. - Copy your function code: Add
COPY index.js .to the image. - Set the Lambda runtime: Add
CMD ["index.handler"]to your Dockerfile. - Build the image: Run
docker build -t playwright-lambda .. - Push to Amazon ECR: Create an ECR repository, authenticate, and push your image using the AWS CLI.
- 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-sandboxflag – 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-coreand manage the browser binary separately to keep your package lean. - Set
PLAYWRIGHT_BROWSERS_PATH=0in 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:18and 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_PATHis 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.