Running Chrome with no screen (“headless”) on AWS Lambda, Amazon’s service for running a function on demand with no server to manage, is a solved problem. Running Chrome with a screen (“headful”), with real pixels being drawn, is not. Chrome refuses to start without a display to draw on, and a Lambda function does not have one. chrome-lambda is a reference container image that gives it a fake one.
This post covers:
- when a browser with a screen is the only option;
- the pieces the image is built from, and the order in which the startup script creates the virtual display before Chrome starts;
- running it locally, and the earlier writeups it builds on;
- when to reach for it, and when plain headless Chrome is the better choice.
When a browser without a screen is not enough
Most Chrome-on-Lambda jobs are served by chrome --headless. The cases that are not:
- Browser extensions that will not load in headless mode, such as ad-blockers and corporate sign-in extensions.
- Websites that detect a headless browser and quietly serve it a stripped-down page.
- Work that needs real pixels: screenshots of the page as actually rendered, PDFs with web fonts laid out correctly, and image comparisons that catch visual regressions.
Each of those needs a real Chrome, which means a real display, inside the 10 GB container image Lambda allows.
The recipe
The repo is a fork of Guillaume Ruch’s chrome-lambda. It adds the flags Chrome needs to start, puts the display on :99 and points HOME at /tmp, the only writable folder on Lambda. The image is a handful of familiar pieces:
- Ubuntu 20.04, plus AWS’s Lambda runtime interface client, which lets any container image act as a Lambda function.
- Xvfb, the “X virtual framebuffer”: a display server that draws into memory instead of a monitor. It gives Chrome a display to open, numbered
:99, with no graphics card behind it. A small window manager, Fluxbox, runs on top. google-chrome-stablefrom Google’s package repository, started bychrome-launcherand driven over Chrome’s DevTools debugging protocol. It runs with--no-sandbox,--disable-dev-shm-usageand--no-xshm, the usual flags for Chrome in a locked-down container.
The script that runs when the container starts does three things in order. It sets the DISPLAY environment variable to :99 and starts Xvfb there, waiting up to five seconds for it to answer. It starts Fluxbox the same way. Then it hands control to Lambda’s runtime client, which calls the handler that launches Chrome. Chrome never learns the display is fake.
The sample handler only proves the browser works. It opens example.com and reads chrome.runtime from the page. An extension.zip sits in the repo for you to load.
Running it locally
When the container is not running on Lambda, the startup script wraps the handler in AWS’s runtime interface emulator, which listens on port 8080:
docker build -t chrome-lambda .
docker run -p 9000:8080 chrome-lambda
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
Reading material
The image stands on three earlier writeups, all linked from the README:
- Running headful Chrome with extensions in a Lambda container image
- Running Chrome in a Docker container
- Run Xvfb on AWS Lambda container
When to reach for it
If a site serves a different page to headless browsers, or you need a privacy or sign-in extension preloaded, the repo is a starting point. For everything else, headless Chrome is lighter and cheaper, and you should use it.
