Using Playwright with AWS Lambda
Using Playwright with AWS Lambda
Playwright is a powerful Node.js library for browser automation, commonly used for web scraping, end-to-end testing, and automation tasks. While it's designed to run on full-featured systems, you can also run it on AWS Lambda — with a few workarounds.
Running Playwright in Lambda is a great solution for:
Headless browser automation
Scheduled scraping jobs
Automated testing in the cloud
Monitoring UI behavior
✅ Can You Run Playwright on AWS Lambda?
Yes, but not directly with a standard AWS Lambda runtime. Playwright requires a headless Chromium browser, which is not natively available in AWS Lambda. You’ll need to use:
A custom AWS Lambda layer, or
A container image (preferred method) that includes Playwright and its dependencies.
π§ Recommended Setup: AWS Lambda with Container Image
π Step 1: Create a Dockerfile
Dockerfile
Copy
Edit
FROM public.ecr.aws/lambda/nodejs:20
# Install dependencies
RUN yum install -y wget xz unzip
# Install Playwright and its dependencies
RUN npm install -g playwright && \
playwright install --with-deps
# Copy function code
COPY app.js ./
# Command to run the function
CMD [ "app.handler" ]
This Dockerfile installs Node.js, Playwright, and required system dependencies.
π Step 2: Create app.js (Your Lambda Function)
javascript
Copy
Edit
const playwright = require('playwright');
exports.handler = async (event) => {
const browser = await playwright.chromium.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
const title = await page.title();
await browser.close();
return {
statusCode: 200,
body: JSON.stringify({ title }),
};
};
π§ͺ Step 3: Build and Deploy the Lambda
bash
Copy
Edit
docker build -t playwright-lambda .
docker tag playwright-lambda:latest <your_aws_account_id>.dkr.ecr.<region>.amazonaws.com/playwright-lambda
aws ecr create-repository --repository-name playwright-lambda
aws ecr get-login-password | docker login --username AWS --password-stdin <your_ecr_url>
docker push <your_ecr_url>/playwright-lambda
Then create a Lambda function using this container image via the AWS Console or CLI.
⚙️ Alternative: Use AWS Lambda with Layers
If you don’t want to use containers, you can:
Use a custom Lambda layer that includes Chromium and Playwright.
Bundle your function with those dependencies.
⚠️ This approach is more complex due to Lambda size and execution time limits.
⏱️ Limits to Consider
Limit Note
Memory 1024–3008 MB is recommended
Timeout Max 15 minutes
Cold starts Slightly longer with headless browser init
Package size (non-container) Max 250 MB (unzipped)
π IAM Role Permissions
Your Lambda function may need access to:
ECR (if using container images)
CloudWatch Logs
Secrets Manager / S3 (if storing credentials or output)
✅ Best Practices
Use containers to simplify dependency management.
Clean up temporary files and close browsers to save memory.
Use layers or scratch space for larger Chromium builds if not using containers.
Learn Playwright Training Course in Hyderabad
Read More
Running Playwright Tests on BrowserStack
Integrating Playwright with Azure DevOps
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment