Playwright Screenshots in AWS Lambda: Step-by-Step
This guide explains how to capture screenshots with Playwright inside an AWS Lambda function. You’ll learn how to set up a Lambda that runs a headless Chromium browser, navigates to a URL, and saves a screenshot to S3. This is useful for automated monitoring, generating preview images, or archiving web pages. You’ll need an AWS account, basic familiarity with the Lambda console, and Node.js installed locally for packaging.
What You’ll Need
- An AWS account with permissions to create Lambda functions, IAM roles, and S3 buckets.
- AWS CLI installed and configured (optional but helpful).
- Node.js and npm installed on your local machine.
- An S3 bucket where screenshots will be stored.
Step-by-Step
- Create an S3 bucket (if you don’t have one) to store the screenshots. Note the bucket name and region.
- Create an IAM role for the Lambda with permissions to write to S3 and to write logs to CloudWatch. Attach the managed policies
AmazonS3FullAccess(or a scoped-down version) andAWSLambdaBasicExecutionRole. - Set up a local project directory and initialize it with
npm init -y. - Install Playwright as a dependency:
npm install playwright-core(useplaywright-coreto avoid downloading browsers automatically). - Download the Chromium browser that matches the Playwright version. Run
npx playwright install chromiumto get the browser locally, then copy the browser binary from the cache (usually under~/.cache/ms-playwright) into your project folder, e.g.,./chromium. - Write the Lambda handler (e.g.,
index.js) that uses Playwright to launch Chromium with the executable path pointing to your bundled browser. The handler should accept an event with aurland optionally afilename. - Package the code into a ZIP file. Include the
node_modulesfolder and the Chromium binary. Because Lambda has a 250 MB unzipped limit, you may need to strip unnecessary files from the browser (e.g., remove locales and debug symbols). - Create the Lambda function in the AWS Console (or via CLI). Choose Node.js 18.x or 20.x runtime, set the handler to
index.handler, and upload the ZIP. - Adjust the Lambda configuration: set the memory to at least 1024 MB (Playwright is memory-hungry) and the timeout to 30 seconds or more, depending on the page load time.
- Test the function with a sample event containing a URL. Check the CloudWatch logs for errors and verify the screenshot appears in your S3 bucket.
Common Mistakes
- Using the full Playwright package instead of
playwright-core. The full package tries to download browsers at install time, which is not allowed in Lambda. - Not setting the executable path correctly. Playwright needs to know where the Chromium binary is located inside the Lambda environment.
- Missing system dependencies for Chromium in Lambda. The Amazon Linux 2 environment lacks many libraries. You must bundle the necessary
.sofiles or use a pre-built layer. - ZIP file too large or includes unnecessary files. Keep the browser minimal and exclude source maps, tests, and other bloat.
- Insufficient memory or timeout. If the Lambda times out or runs out of memory, increase both settings.
Tips
- Use a Lambda layer to share the Chromium binary across functions. There are community layers available, but you can create your own.
- Set the Lambda environment variable
PLAYWRIGHT_BROWSERS_PATHto0to force Playwright to use the bundled browser path. - Consider using
chromium.launch({ args: ['--no-sandbox', '--disable-gpu'] })because Lambda runs as root and doesn’t have a GPU. - For faster cold starts, keep the browser binary in a Lambda layer and attach it to the function.
- Use the
waitUntil: 'networkidle'option when navigating to ensure the page is fully loaded before taking the screenshot.
Troubleshooting
- Error: “Executable doesn’t exist” – Check the executable path in your code. It should point to the location where you placed the Chromium binary (e.g.,
/var/task/chromium/chrome-linux/chrome). - Error: “Missing libraries” – When launching Chromium, you may see errors about missing shared libraries like
libnss3.soorlibatk-1.0.so. You need to bundle these libraries or use a pre-built layer. One workaround is to use a Docker image that mimics Lambda and install the dependencies there. - Lambda times out – Increase the timeout in the Lambda configuration. Also, consider using a smaller page or optimizing the wait condition.
- Memory limit exceeded – Increase the memory allocation. Chromium can easily use 300-500 MB.
- Screenshot is blank or black – Ensure you wait for the page to render. Use
page.waitForTimeout()or wait for a selector. Also, check if the page uses canvas or WebGL that might not render headless. - ZIP file too large to upload – Use the AWS CLI to upload the ZIP if it’s >50 MB, or use an S3 bucket as an intermediary.
Frequently Asked Questions
Can I use Puppeteer instead of Playwright?
Yes, Puppeteer works similarly, but Playwright has better cross-browser support and a cleaner API. The setup steps are analogous.
How do I take a full-page screenshot?
Use page.screenshot({ fullPage: true }). Be aware that very long pages may require more memory and time.
Can I capture screenshots of pages that require login?
Yes, you can use page.goto() to navigate to the login page, fill in credentials, submit, and then navigate to the target page. Store credentials in AWS Secrets Manager or environment variables.
What is the cost of running this on Lambda?
Cost depends on the number of invocations, memory allocated, and duration. A single screenshot with 1024 MB memory and 10 seconds would be a fraction of a cent. Free tier includes 400,000 GB-seconds per month.
How do I trigger the Lambda to take a screenshot?
You can trigger it via an API Gateway endpoint, a scheduled CloudWatch Event, or an S3 event. The event payload should include the URL and optionally the output filename.
Can I use the screenshot in a web app?
Yes, after saving to S3, you can make the object public or generate a pre-signed URL to share it securely.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.