How to configure headless browser location for Playwright Python on AWS Lambda

I’ve been working with Playwright using JavaScript before and it was straightforward because there was an npm library (playwright-aws-lambda) that handled everything automatically.

Now I’m trying to switch to Python but I can’t find a similar package that works the same way. I’m not sure where to begin with the manual setup.

Does anyone know if there’s a Python package that supports Playwright on AWS Lambda? Or maybe someone can point me toward a basic approach to get a headless browser working in this environment?

Just to be clear, I need to stick with Playwright specifically, so other browser automation tools won’t work for my use case.

Hit this same issue 6 months back during a scraping pipeline migration. The playwright-python-lambda package from PyPI solved it for me, though it’s rougher than the JS version. You’ll manually download the chromium binary and bundle it with your deployment. The annoying part is getting PLAYWRIGHT_BROWSERS_PATH set right and making sure the binary can execute. I used playwright install chromium during build, then copied binaries to a specific directory structure. Memory was tight with Lambda’s limits, so I had to tweak browser launch options carefully. More work than JS setup but totally doable once you nail the paths.

i tried this last month and went with docker containers instead of layers. way easier than messing with binary paths manually. just use the official playwright image as your base and deploy through lambda’s container support. works great and you don’t have to deal with executable locations or size limits.

I encountered similar challenges transitioning from Node.js to Python with Playwright. Unfortunately, there isn’t a direct equivalent to playwright-aws-lambda for Python. To resolve this, I created a custom Lambda layer that includes the required Playwright binaries. The key steps involved installing Playwright in a Linux environment that resembles Lambda’s, packaging the binaries alongside your application, and specifying the executablePath in playwright.chromium.launch(). Be aware that the browser binaries can be quite large, so consider optimizing or switching to Lambda’s container images. Additionally, ensure that environment variables are set for headless mode. While it’s more complex than the JavaScript setup, it can definitely be achieved with proper packaging.