User Agents

Web browsers and everyday web tech, explained

Run Playwright in Azure Functions with Managed Browsers

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Running Playwright in an Azure Function using Linux containers and managed browsers allows you to perform headless browser automation, web scraping, and PDF generation without the overhead of managing browser binaries within your function's deployment package. By utilizing a custom Docker container, you ensure all necessary system dependencies for Chromium, Firefox, or WebKit are present, while the "managed browser" approach typically involves connecting to a remote browser instance (like Playwright Service or a separate browserless.io container) to avoid the memory and CPU constraints of the Azure Functions consumption plan.

What You'll Need

  • An Azure account with an active subscription.
  • Azure Functions Core Tools installed locally.
  • Docker Desktop installed and running.
  • An Azure Container Registry (ACR) to host your custom image.
  • A Linux-based Azure Function App plan (Premium or Dedicated plan is recommended for stability).
  • A remote browser connection string (WebSocket URL) if using a managed browser service.

Step-by-Step

  1. Initialize the Project: Create a new Azure Function project using your preferred language (Node.js or Python). Ensure you initialize it as a model that supports custom containers. For Node.js, run func init MyPlaywrightFunction --worker-runtime node.
  2. Install Playwright: Add the Playwright library to your project. Run npm install playwright. If you are using a managed browser service, you only need the core library; you do not need to run npx playwright install locally, as the browsers will not run inside the Function's execution environment.
  3. Create the Dockerfile: Create a Dockerfile in the root directory. Use the official Microsoft Azure Functions base image. To ensure the environment can handle browser-related networking and system calls, use a Debian-based image. Example: FROM mcr.microsoft.com/azure-functions/node:4-node18.
  4. Configure System Dependencies: Even when using managed browsers, some system libraries are required for the Playwright driver to communicate. Add a command to your Dockerfile to update the package manager and install basic dependencies: RUN apt-get update && apt-get install -y libglib2.0-0 libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1 libasound2.
  5. Implement the Connection Logic: In your function code, instead of using browserType.launch(), use browserType.connect(). Pass the WebSocket URL of your managed browser service (e.g., wss://chrome.browserless.io?token=YOUR-TOKEN) as the endpoint. This offloads the heavy browser process to an external server.
  6. Build and Push the Image: Build your Docker image locally using docker build -t myregistry.azurecr.io/playwright-func:v1 .. Then, push the image to your Azure Container Registry: docker push myregistry.azurecr.io/playwright-func:v1.
  7. Deploy the Function App: Create the Azure Function App in the portal or via CLI. Select "Docker Container" as the deployment method. Point the app to the image hosted in your ACR.
  8. Set Environment Variables: In the Azure Portal, navigate to Configuration > Application Settings. Add a setting for BROWSER_WSS_ENDPOINT containing your managed browser URL to keep the connection string out of your source code.

Common Mistakes

  • Attempting Local Launch: Trying to use browser.launch() inside a standard Azure Function consumption plan usually fails because the environment lacks the required shared libraries and exceeds the memory limit.
  • Missing Dependencies: Forgetting to include the apt-get install commands in the Dockerfile, which leads to "shared library not found" errors when the Playwright driver attempts to initialize.
  • Timeout Issues: Using the default Azure Function timeout (5 minutes). Browser automation is often slow; ensure you increase the function timeout in host.json or move to a Premium plan for longer execution windows.
  • Hardcoding Credentials: Putting the WebSocket token directly in the code instead of using Azure App Settings.

Tips

  • Use Headless Mode: Always ensure the browser is running in headless mode to minimize resource consumption.
  • Implement Resource Cleanup: Always wrap your browser and context logic in try...finally blocks to ensure await browser.close() is called, preventing memory leaks in the managed browser cluster.
  • Optimize Image Size: Use multi-stage builds in your Dockerfile to keep the final production image slim, reducing deployment time and cold start latency.
  • Monitor Memory: Use Azure Application Insights to track the memory usage of your function to determine if you need to scale up your App Service Plan.

Troubleshooting

If your function fails to connect to the managed browser, first verify the WebSocket URL. Test the connection from your local machine using the same code to rule out network issues. If you see "Protocol Error" or "Connection Closed," check if the managed browser service has reached its concurrency limit.

If the container fails to start, check the logs in the "Log Stream" section of the Azure Portal. Common failures involve incorrect base images or missing dependencies in the Dockerfile. If the function times out, check the host.json file and ensure the functionTimeout is set to a value that accommodates the page load times of the target website.

Frequently Asked Questions

Can I run Playwright without a managed browser service?

Yes, but you must use a custom Docker container with all browser binaries installed and use a Premium or Dedicated Azure Function plan to provide enough RAM and CPU for the browser process.

Which browser is best for Azure Functions?

Chromium is generally the most stable and widely supported for headless automation in Linux containers.

Does this work on the Consumption plan?

It is highly discouraged. The Consumption plan has strict memory limits and lacks the system dependencies required for browser binaries, making managed browsers or Premium plans necessary.

How do I handle CAPTCHAs in this setup?

Managed browser services often provide plugins or integration points for CAPTCHA solving services, which are easier to implement than trying to solve them within a serverless function.

Is it possible to use Playwright with Python in this setup?

Yes, the process is identical. You would use a Python-based Azure Function base image and the playwright-python library instead of the Node.js version.