Running a Playwright script on AWS Lambda
On This Page Why Run Playwright on AWS Lambda?January 26, 2026 · 15 min read · Tool Comparison
Have you always tried run a uncomplicated script on AWS Lambda, expecting it to & # 8220; just work & # 8221;? I did. And it didn & # 8217; t employment. What looked like a small undertaking quickly ran into browser limitations, miss habituation, and runtime constraints. Lambda seemed ideal for lightweight automation, but the setup was far from straightforward. As our needs grew, it turn open that Playwright could run on Lambda-but not without the right configuration. With the proper approach, though, Lambda can become a fast, scalable, and cost-efficient way to execute Playwright scripts on demand. Running a Playwright script on AWS Lambda allows you to execute browser automation in a serverless environment without care infrastructure. With the right setup, Lambda can treat lightweight Playwright chore efficiently and on requirement. Key Steps and Considerations Best Practices This article explores how to run Playwright scripts on AWS Lambda, continue deployment options, configuration stairs, and better practices for reliable execution. Running Playwright on AWS Lambda allows you to execute browser mechanisation without managing host. For job such as generating screenshots, creating PDFs, submitting pocket-sized forms, or performing quick data extraction, Lambda provides an efficient environs that lead only when triggered. This helps reduce both cost and usable effort. AWS Lambda is also designed to scale automatically. It can handle a few execution or a large number of parallel invocations with no additional form. This makes it a potent fit for event motor workflows or workloads with unpredictable traffic. Playwright aligns well with this model because of its ability to run in headless mode with minimal overhead. With proper frame-up, Lambda becomes a practical option for running Playwright handwriting faithfully. Key benefits include: These advantages make AWS Lambda an appealing choice for teams looking to run Playwright scripts in a serverless and cost-efficient mode. However, as testing needs increase and browser coverage becomes more demanding, yet Lambda can face limitations. In scenarios where you require real device, panoptic browser support, or high-volume parallel execution, offers a scalable alternative. It provides ready-to-use real environments that can complement or extend your serverless Playwright workflows. Running Playwright on AWS Lambda is potential, but it introduces constraints that are not present in traditional server environments. Lambda & # 8217; s circumscribed runtime, storage, and dependance support can affect how reliably a headless browser starts and executes. Common challenges include: Understanding these challenges helps you select the right deployment method and configure Playwright to run swimmingly in a serverless environment. Read More: Before you deploy Playwright scripts to AWS Lambda, you need a few tools, services, and canonical configurations in place. These assure that you can package dependency right and manage your map without unexpected issues. You should have: Having these prerequisites ready will make it much easier to choose a deployment method and travel swimmingly into the configuration and packaging measure. Read More: Before running Playwright on AWS Lambda, it is important to decide how you will package the browser binaries and dependencies. Lambda does not include these components by default, so choosing the right deployment method determines how smoothly your handwriting will run. There are two independent approaching: 1. AWS Lambda Layers Lambda Layers allow you to box Playwright and its browser dependencies singly and attach them to your function. This keeps your function codification smaller and easier to cope. However, Layers hold sizing limit, and preparing them correctly can be complex due to the declamatory Chromium binary Playwright requires. 2. AWS Lambda Container Images(Recommended) Container images provide a more flexible way to include everything Playwright needs. By using a Docker icon, you can install all required browsers and library without worry about Lambda & # 8217; s sizing restrictions. This method oftentimes results in more reliable browser launches and simpler maintenance over time. How to Decide Selecting the correct method early helps ensure your Playwright scripts run consistently and reduces deployment complexity. Read More: To run Playwright dependably on AWS Lambda, you need to adjust how it is configure. The goal is to keep the browser lightweight, the script predictable, and the execution time within Lambda limit. The first footstep is to do sure Playwright always runs in headless mode. Lambda execute not have a showing environment, so a seeable browser is neither needed nor supported. Headless mode also reduces startup clip and resourcefulness use. Where potential, use a single browser engine such as Chromium for Lambda. This keeps the bundle smaller and simplifies dependency direction. You can still use early browsers in local or CI environments, but Lambda benefits from a minimum and consistent setup. It is too crucial to tune timeouts and retries for a serverless context. Lambda map can be slower to get, and network calls may occasionally guide longer than on a local machine. Increasing timeouts slightly and enabling a minor number of retries for key actions can make your scripts more stable. Configuration details to consider include: By treating serverless as a first class environment in your Playwright configuration, you reduce surprisal and do it much easy to deploy the same scripts consistently on AWS Lambda. Read More: AWS Lambda Layers let you package shared dependencies formerly and reuse them across multiple functions. For Playwright, this signify order the Playwright library and its browser binary inside a bed, then attaching that layer to any Lambda function that involve to run browser automation. Because Playwright and Chromium are relatively large, this approach works better for modest apparatus and when you stick within Lambda & # 8217; s size limit. To use a Lambda Layer for Playwright, you will: For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users. On a machine that matches the Lambda runtime (for example, Amazon Linux for the Node.js runtime): Example: npm init -y At this point, the nodejs folder contains node_modules with Playwright and Chromium dependencies. From the playwright-layer directory, create a ZIP file: Then, in the AWS Console: After the layer is create, remark its ARN. You will attach this ARN to your Lambda function. In the Lambda purpose configuration: Your part can now import and use Playwright in its handler without pack it forthwith in the part code bundle. If your Playwright setup grows larger or you involve more control over dependencies, a container based deployment is usually more flexible, which is why it is recommended as the second option. Read More: Using AWS Lambda container images is the most reliable and flexile way to run Playwright on Lambda. This method withdraw many of the sizing and dependency limit you encounter with Lambda Layers. With a container image, you control the entire runtime surroundings, include browser binaries, system libraries, and Node.js versions. Using container images cater several advantages when running Playwright on AWS Lambda: This makes container image the most reliable and scalable option for deploying Playwright scripts on AWS Lambda. The summons of preparing your Playwright container for AWS Lambda involves a few straightforward steps: Start by using an official Playwright bag image that include the necessary browser dependencies: WORKDIR /app COPY package * .json ./ COPY . . CMD [& # 8220; index.handler & # 8221;] This ensures that Playwright and Chromium run correctly inside Lambda & # 8217; s surround. Once the Dockerfile is ready, build and tag your image: Authenticate to Amazon ECR, create a monument if necessitate, and push the persona: docker tag playwright-lambda: latest .dkr.ecr.amazonaws.com/playwright-lambda: up-to-the-minute In the AWS Console: After deployment, configure retentivity and timeout scope based on your Playwright script & # 8217; s necessity. Using container images is idealistic when you want: This approach provides the most predictable experience for running Playwright hand on AWS Lambda and is well suited for production workload. Read More: The handler is the entry point of your Lambda role. This is where you launch Playwright, run your script, clean up resources, and return a answer or store outcome. A full handler should: Below is a simple example in Node.js that opens a page, takes a screenshot, and returns a canonical response. In a real project, you might store the screenshot in S3 or process data instead. exports.handler = async (event) = & gt; { try { const page = await browser.newPage (); const url = event.url || & # 8216; https: //example.com & # 8217;; // Example action: take a screenshot return { return { Key point to continue in mind: This coach pattern gives you a clean and predictable way to run Playwright inside AWS Lambda. Read More: Running Playwright on AWS Lambda works best when the map is tune for speed and stability. The goal is to reduce startup time, stay within resource boundary, and create each invocation as efficient as possible. Here are key areas to concentre on: 1. Tune Memory and Timeout Settings 2. Keep the Container or Package Lean 3. Optimize the Playwright Script 4. Use the/tmpDirectory Wisely 5. Handle Cold Starts and Concurrency 6. Monitor and Adjust Based on Real Usage By applying these practices, you can get Playwright scripts run more expeditiously on AWS Lambda and cut failures stimulate by timeouts, resource limit, or unnecessary overhead. Even with these optimizations, Lambda is still best suited for lightweight Playwright task. As exam complexity grows, you may need broader browser coverage, real devices, or higher parallel execution than Lambda can comfortably support. In such cases, extending your setup with a cloud testing platform becomes a hardheaded succeeding step. When testing needs go beyond what AWS Lambda can reliably support-such as broad browser reporting, real mobile device, or high parallel execution-BrowserStack Automate become a natural propagation to your Playwright workflow. Instead of pass browsers inside Lambda, your Playwright book can connect directly to BrowserStack & # 8217; s cloud base, where tryout are executed on real background and mobile environments. In this model, Lambda or your CI scheme simply serves as the test runner, while BrowserStack deal the execution bed. Key capableness include: By borrow BrowserStack Automate alongside your Lambda-based Playwright frame-up, you ensure your screen infrastructure remains both flexible and robust-ready for event-driven chore and large scale regression suite alike. Running Playwright on AWS Lambda offers an efficient way to accomplish lightweight mechanisation tasks without maintaining host. With the correct conformation, Lambda can launch headless browser, perform aim action, and scale on demand, get it well-suited for event-driven or occasional automation workflows. However, Lambda has open limits around executing time, memory, and browser packaging. For broader test coverage,, or large, lead your setup with a cloud testing platform becomes all-important. Combining Lambda & # 8217; s on-demand compute with the scalability and device coverage of BrowserStack Automate gives you a flexible approach that supports both small serverless job and larger, production-grade examination needs. By read the strength of each program and using them where they fit best, you can build a Playwright testing workflow that is fast, true, and ready to scale as your application grows. Tool Comparisons: On This Page # Ask-and-Contributeabout this topic with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.Running a Playwright script on AWS Lambda
Overview
Why Run Playwright on AWS Lambda?
Challenges of Running Browser Automation on Lambda
Prerequisites for Deploying Playwright on AWS Lambda
Choosing the Right Deployment Method
Configuring Playwright for Serverless Execution
Deploying Playwright Using AWS Lambda Layers (Option 1)
Workflow
Creating the layer contents
mkdir -p playwright-layer/nodejs
cd playwright-layer/nodejs
npm install playwright
npx playwright install chromiumPackaging and uploading the layer
cd ..
zip -r playwright-layer.zip nodejsAttaching the layer to a Lambda function
When to Use This Approach
Deploying Playwright Using AWS Lambda Container Images (Option 2 & # 8211; Recommended)
Why this method is recommended
What You Do When Creating the Container
Creating the Dockerfile
FROM mcr.microsoft.com/playwright: v1.42.0-jammy
RUN npm installBuilding and pushing the container
docker build -t playwright-lambda.
aws ecr get-login-password & # 8211; region us-east-1
| docker login & # 8211; username AWS & # 8211; password-stdin .dkr.ecr.amazonaws.com
docker push .dkr.ecr.amazonaws.com/playwright-lambda: up-to-the-minuteDeploying the container to Lambda
When to use this method
Writing a Playwright Handler Function for AWS Lambda
// index.js
const {chromium} = require (& # 8216; playwright & # 8217;);
let browser;
browser = await chromium.launch ({
headless: true,
});
await page.goto (url, {waitUntil: & # 8216; networkidle & # 8217;});
await page.screenshot ({route: & # 8216; /tmp/page.png & # 8217;});
statusCode: 200,
body: JSON.stringify ({
content: & # 8216; Playwright script completed successfully & # 8217;,
urlVisited: url,
}),
};
} match (error) {
console.error (& # 8216; Error running Playwright in Lambda: & # 8217;, error);
statusCode: 500,
body: JSON.stringify ({
message: & # 8216; Playwright hand failed & # 8217;,
error: error.message,
}),
};
} finally {
if (browser) {
await browser.close ();
}
}
};Optimizing Playwright Performance on AWS Lambda
Scale Playwright Testing Beyond Lambda with BrowserStack Automate
Conclusion
Useful Resources for Playwright
Related Guides
Automate This With SUSA
Test Your App Autonomously