Guide
Run Playwright on Lambda — without shipping a browser
Bundling Chromium into a serverless function means fighting package size limits, missing system libraries and slow cold starts. Laptops have the opposite problem: they sleep, change networks and run out of RAM at 20 tabs.
The fix is the same for both: keep your agent code where it is, and run the browser somewhere else.
The pattern
- Install
playwright-core(client only, no browser download). - Connect to a remote browser over CDP with your token in a header.
- Always close the browser in
finallyso the session ends with the invocation.
Lambda handler
shell
npm i playwright-corehandler.mjs
// handler.mjs — Lambda, Node 20 runtime
import { chromium } from "playwright-core";
export const handler = async (event) => {
const browser = await chromium.connectOverCDP("wss://connect.crawlflux.dev", {
headers: { Authorization: `Bearer ${process.env.CRAWLFLUX_TOKEN}` },
});
try {
const ctx = browser.contexts()[0] ?? (await browser.newContext());
const page = await ctx.newPage();
await page.goto(event.url ?? "https://example.com", { waitUntil: "domcontentloaded" });
return { statusCode: 200, body: JSON.stringify({ title: await page.title() }) };
} finally {
await browser.close();
}
};Set CRAWLFLUX_TOKEN as an environment variable (or pull it from Secrets Manager). Give the function a timeout longer than your slowest page load; memory can stay at the minimum because no browser runs locally.
Other runtimes
- Vercel / Netlify functions: same code; mind the platform's max duration.
- Cloudflare Workers: Playwright's Node client does not run on workerd. Drive the CDP WebSocket directly or call a small Node service.
- Laptop / CI: point existing test suites at the remote endpoint to run more in parallel than your machine can hold.
Common failures
401 Unauthorizedon connect — header missing or token rotated.Target closedmid-run — session hit the idle or 30-minute limit; split long jobs.- Function times out but session keeps running — you skipped
browser.close(); the idle timeout will reap it.
See the full CDP quickstart or get a token.