DEV Community

Cover image for Running headless Chromium in Azure Functions with Puppeteer and Playwright

Running headless Chromium in Azure Functions with Puppeteer and Playwright

Anthony Chu on August 18, 2020

With a recent update to Azure Functions, it is now possible to run headless Chromium in the Linux Consumption plan. This enables some serverless br...
Collapse
 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ

Thanks for publishing this article it really helped me.

I went through the steps, except that I upload a zip from CI, since my function references files outside the functions root dir, and I get:

browserType.launch: Failed to launch browser: Error: spawn /home/site/wwwroot/node_modules/playwright/.local-browsers/chromium-827102/chrome-linux/chrome EACCES

I wonder if I'm missing a step to configure the user access.

Collapse
 
sujithsshetty profile image
Sujith Shetty

Hi @bennypowers , did you find fix for this?

Collapse
 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ

IIRC you need to build it on the remote

func azure functionapp publish my-playwright-func  --build remote
Enter fullscreen mode Exit fullscreen mode

I think you may also need to set an ENV var which specifies that chromium should install in the repo working dir, or something like that

Collapse
 
ronkot profile image
Jaakko Juvonen • Edited

Hi,

Thank you for a very helpful article! With your help I've been almost able to make playwright to work on linux consumption plan. The problem that still exists seems to be that the function does not have access to start the browser process:

Error: browserType.launch: Failed to launch: Error: spawn /home/site/wwwroot/node_modules/playwright-chromium/.local-browsers/chromium-907428/chrome-linux/chrome EACCES)

Googling shows that this problem might arise only when deploying through azure devops pipeline, which I'm doing.

Do you or anyone else have any ideas on what could be the solution for this problem?

Thousand thanks! ๐Ÿ™‡โ€โ™‚๏ธ

Collapse
 
akhilkrishna11 profile image
Akhil krishna

Hi @ronkot , Did you get any solution for this? Please help me out. I am also facing this issue. Please have a look on the below error.

An unexpected error : { browserType.launch: Failed to launch: Error: spawn /home/site/wwwroot/node_modules/playwright-core/.local-browsers/chromium-930007/chrome-linux/chrome EACCES

Collapse
 
ronkot profile image
Jaakko Juvonen • Edited

Unfortunately I couldn't fix the issue. Finally I just gave up using playwright. I'd liked to use Playwright to print html page to pdf, but I refactored my system to use a distinct js-only mechanism to generate PDFs using pdfmake.

Collapse
 
darshanrampatel profile image
Darshan

Hi Anthony,

I'm unable to get either the puppeteer/playwright functions to run successfully in Azure - they run fine locally, but once deployed, they fail with the below error:

Result: Failure Exception: Worker was unable to load function GeneratePDF: 'Error: Cannot find module 'playwright-chromium' Require stack: - /home/site/wwwroot/GeneratePDF/index.js - /azure-functions-host/workers/node/worker-bundle.js - /azure-functions-host/workers/node/dist/src/nodejsWorker.js' Stack: Error: Cannot find module 'playwright-chromium' Require stack: - /home/site/wwwroot/GeneratePDF/index.js - /azure-functions-host/workers/node/worker-bundle.js - /azure-functions-host/workers/node/dist/src/nodejsWorker.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15) at Function.Module._load (internal/modules/cjs/loader.js:841:27) at Module.require (internal/modules/cjs/loader.js:1025:19) at require (internal/modules/cjs/helpers.js:72:18) at Object. (/home/site/wwwroot/GeneratePDF/index.js:1:22) at Module._compile (internal/modules/cjs/loader.js:1137:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10) at Module.load (internal/modules/cjs/loader.js:985:32) at Function.Module._load (internal/modules/cjs/loader.js:878:14) at Module.require (internal/modules/cjs/loader.js:1025:19)

Any ideas?

Collapse
 
tjrobinson profile image
Tom Robinson • Edited

@Darshan - did you ever manage to resolve this? I'm getting the same thing.

Edit: I solved it using some of the advice here (stackoverflow.com/questions/639499...), and by making sure I deployed by right-clicking on my project in VS Code and choosing "Deploy to Function App..." rather than doing it at the command-line.

Collapse
 
darshanrampatel profile image
Darshan

@tom , no I didn't in the end - but glad you got it to work in the end. When I get a chance I will try and get mine to work as well!

Collapse
 
johnnliu profile image
John Liu ๅŠ‰

When deploying from VSCode, "azureFunctions.scmDoBuildDuringDeployment": true conflicts with

Error: Run-From-Zip is set to a remote URL using WEBSITE_RUN_FROM_PACKAGE or WEBSITE_USE_ZIP app setting. Deployment is not supported in this configuration.

Error: Linux consumption plans only support zip deploy. See here for more information.

I'm not sure if I missed a step.

Collapse
 
anthony profile image
Anthony Chu

Is this a new Linux Consumption plan that you're deploying to? I didn't run into this with a new app. This is likely because one of those settings were previously added to the app. You should be able to delete that app setting and try again. Let me know if it works.

Collapse
 
prasunakunasani profile image
Prasuna

Hi Anthory,
Is there a similar way to get this working using selenium webdriver? Things work fine locally but once deployed, I keep running into 'Exception: WebDriverError: unknown error: cannot find Chrome binary'

Collapse
 
muhaym profile image
Abdul Muhaymin Arif

Any idea whether we can do remote build from Azure Devops Release Pipeline?

Collapse
 
gfoley83 profile image
Gavin Foley

Hi Anthony. Is there any ETA for adding the ability to run headless Chromium from App Service Plans?

Collapse
 
fabioh profile image
Fรกbio Henrique Gabriele

The article was amazing.
Can I use this solution with a function running in a kubernetes cluster with Keda?

Collapse
 
elglogins profile image
Elgars Logins

Is this working same way for v4 functions and node 16 lts?