Prompt
How do I run headless browser jobs in the cloud?
Latest observation
Running headless browser jobs in the cloud usually means you want to automate a real browser (Chrome/Chromium/Firefox) without a visible UI for tasks like scraping, screenshots, PDF generation, testing, or form automation.
Common ways to do it
1) Use a managed browser automation service
Best if you want the least ops work.
Examples:
- Browserless
- Playwright Cloud / hosted runners (depending on provider)
- Apify
- ScrapingBee / similar browser APIs
- LambdaTest / BrowserStack for testing
How it works:
- Your code runs in your app or serverless function
- It connects to a remote headless browser over WebSocket/CDP
- The provider handles scaling, browser binaries, and sandboxing
Good for:
- Small to medium automation workloads
- Teams that don’t want to manage containers or browser setup
- Bursty workloads
2) Run browsers in containers on cloud VMs / Kubernetes
Most flexible and common for production.
Typical stack:
- Docker image with Chromium/Chrome + Playwright/Puppeteer
- Deployed on:
- AWS ECS/Fargate
- Kubernetes (EKS/GKE/AKS)
- Google Cloud Run
- Azure Container Apps
- Fly.io / Render / Railway (for lighter workloads)
How it works:
- Each job starts a container
- The container launches a headless browser
- The job runs, produces output, then exits
Good for:
- Custom logic
- Reliable production jobs
- Scaling many workers
Important details:
- Install browser dependencies in the image
- Use
--no-sandboxonly if necessary and understand the security tradeoff - Give enough memory/CPU; browsers are heavy
- Prefer ephemeral containers for isolation
3) Use serverless functions with browser support
Works for short jobs, but with constraints.
Options:
- AWS Lambda with Chromium packages
- Google Cloud Functions/Run
- Azure Functions
Typical use:
- Quick screenshots
- Simple scraping
- Lightweight automation
Limitations:
- Cold starts
- Memory/timeout limits
- Browser startup overhead
- Harder for long-running or interactive flows
Recommended architecture for production
A solid pattern is:
- API or queue receives job
- Worker pulls job
- Worker launches headless browser
- Worker writes result to storage
- Worker reports status
Use:
- Queue: SQS, Pub/Sub, RabbitMQ, Redis/BullMQ
- Storage: S3, GCS, Azure Blob
- Observability: logs + metrics + tracing
This decouples request handling from browser execution and lets you scale workers independently.
Example with Playwright in a Docker container
Dockerfile
FROM mcr.microsoft.com/playwright:latest
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["node", "index.js"]
Node.js example
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({
headless: true
});
const page = await browser.newPage();
await page.goto('https://example.com', { waitUntil: 'networkidle' });
await page.screenshot({ path: 'example.png' });
await browser.close();
})();
Deploy that container to Cloud Run, ECS, or Kubernetes.
If you use Puppeteer
Puppeteer works similarly, but Playwright is often preferred now because it supports multiple browsers and tends to be more robust for automation.
Operational concerns
Resource usage
Browsers use a lot of:
- RAM
- CPU
- temporary disk space
Plan for:
- 512 MB may be too small for many tasks
- 1–2 GB+ is often safer per browser instance
Concurrency
Don’t overload a single container with too many browsers.
- One browser per worker is simplest
- Or reuse a browser with isolated contexts if appropriate
Security
- Treat pages as untrusted
- Use network egress controls if needed
- Avoid exposing cloud credentials in browser jobs
- Prefer isolated ephemeral workers
Stability
- Set timeouts for navigation and actions
- Retry on transient failures
- Capture screenshots/logs on errors
- Use health checks and worker restarts
Quick decision guide
Choose:
- Managed service if you want speed and simplicity
- Containers on cloud runtime if you want control and scale
- Serverless if jobs are short and occasional
Practical recommendation
If you’re starting today:
- Use Playwright
- Run it in Docker containers
- Deploy on Cloud Run or ECS/Fargate
- Use a queue for jobs
- Store outputs in object storage
If you want, I can give you:
- a minimal Playwright + Docker + Cloud Run example, or
- an AWS ECS/Fargate architecture, or
- a serverless Lambda setup.