Fix Chrome Lambda Lighthouse No Screenshots
In this article
When you run Lighthouse on AWS Lambda with headless Chrome, screenshots often come back empty or missing. This guide explains why that happens and walks you through the exact fixes, so your audit reports include the visual evidence you need.
What You'll Need
- An AWS Lambda function that runs Lighthouse (Node.js runtime recommended)
- Access to the Lambda console or a deployment tool like Serverless Framework or SAM
- Basic familiarity with editing Lambda environment variables and layers
Step-by-Step
- Set the correct Chrome flags. In your Lambda handler, when launching Chrome via Puppeteer or Playwright, add these flags:
--no-sandbox,--disable-setuid-sandbox,--disable-dev-shm-usage, and--single-process. The--disable-dev-shm-usageflag is critical because Lambda's shared memory is small; without it, Chrome crashes and screenshots fail silently. - Increase the Lambda timeout. Lighthouse audits can take 60–90 seconds. Set your Lambda timeout to at least 3 minutes (180 seconds). Go to the Lambda console, select your function, click "Configuration," then "General configuration," and edit the timeout.
- Allocate more memory. Chrome needs at least 1024 MB of memory to run reliably. Set your Lambda memory to 1536 MB or 2048 MB. This also gives Chrome enough headroom to render the page and capture screenshots.
- Use a compatible Chrome binary. If you're using a prebuilt Chrome layer, ensure it's built for Amazon Linux 2 (the Lambda execution environment). You can use the official
chrome-aws-lambdapackage or a community layer. Verify the binary runs by testing your function with a simple page. - Enable screenshot capture in Lighthouse options. In your Lighthouse configuration, pass
disableStorageReset: falseand explicitly request screenshots. For example:const result = await lighthouse(url, { port, output: 'json', onlyCategories: ['performance'], disableStorageReset: false, logLevel: 'info' });Then extract screenshots fromresult.lhr.audits['final-screenshot']andresult.lhr.audits['full-page-screenshot']. - Save screenshots to disk or return them in the response. Screenshots in Lighthouse are base64-encoded strings. If you're returning the audit result as JSON, they're already included. If you need them as files, write them to
/tmp(Lambda's writable directory) and then upload to S3 or return as base64 in your response. - Test locally with the same environment. Before deploying, run your function locally using the Lambda runtime Docker image. This catches issues like missing shared libraries or wrong Chrome paths. Use
docker run -v $(pwd):/var/task lambci/lambda:nodejs14to simulate.
Common Mistakes
- Forgetting the
--single-processflag. Without it, Chrome spawns multiple processes that Lambda's sandbox may kill, causing screenshots to be empty. - Using a Chrome binary built for a different OS. Debian-based Chrome won't run on Amazon Linux. Always use a Lambda-compatible build.
- Setting timeout too low. If the function times out, you get no output at all, not just missing screenshots. Check CloudWatch logs for timeout errors.
- Not handling the Promise correctly. Lighthouse returns a Promise; if you don't await it properly, your function may exit before screenshots are captured.
Tips
- Use the
chrome-aws-lambdanpm package – it bundles the correct Chrome binary and sets recommended flags automatically. - Set
LIGHTHOUSE_CHROMIUM_PATHenvironment variable to the path of your Chrome binary if you're using a custom layer. - For full-page screenshots, ensure the page doesn't have infinite scrolling, as that can cause the screenshot to be cut off.
- Cache the Chrome binary in a Lambda layer to reduce cold start time and avoid re-downloading each invocation.
Troubleshooting
- If you still get no screenshots: Check CloudWatch logs for errors like "Chrome failed to start" or "Protocol error." Add
console.logstatements to see if the Lighthouse result contains the screenshot audits. - If screenshots are black or blank: This often happens when the page uses WebGL or hardware acceleration. Try adding
--disable-gputo the Chrome flags. - If the function times out: Increase the timeout and memory, and consider using a smaller page or disabling heavy resources like images.
- If the Chrome binary is not found: Verify the path in your code and that the layer is properly attached. Test with
which google-chromein a custom runtime if needed.
Frequently Asked Questions
Why do screenshots work locally but not on Lambda?
Local machines have more memory and a different OS. Lambda's sandbox restricts processes and has limited shared memory, so Chrome needs specific flags and adequate resources.
Can I use Puppeteer instead of chrome-aws-lambda?
Yes, but you must install a compatible Chromium binary and set the same flags. Puppeteer's default download is for Debian, so you'll need to use PUPPETEER_SKIP_DOWNLOAD and provide your own binary.
How do I extract screenshots from the Lighthouse result?
Access result.lhr.audits['final-screenshot'].details.data for the base64-encoded PNG. For full-page, use ['full-page-screenshot'].details.screenshot.data.
Do I need to enable screenshots in the Lighthouse config?
By default, Lighthouse captures screenshots if the browser supports it. But ensure you're not passing disableStorageReset: true, which can interfere.
What is the minimum memory for reliable screenshots?
At least 1024 MB, but 1536 MB is safer. Lower memory can cause Chrome to crash or produce partial screenshots.
Can I return screenshots as a file from Lambda?
Yes, write the base64 data to /tmp and then either upload to S3 or return as a base64 string in your HTTP response.