How to bundle headless chromium module with AWS Lambda?

回眸只為那壹抹淺笑 提交于 2020-12-12 10:25:28

问题


I'm attempting to use Puppeteer with Lambda, however, on serverless deploy, the lambda errors out due to exceeding the 250mb unbundled package size limit.

So, to get under the limit, I've switched to Puppeteer core which doesn't come packaged with chromium. This requires referencing a path to an executable to launch chrome. (e.g. puppeteer.launch({executablePath: headlessChromiumPath}));

However, I'm not sure how to load a headless Chromium into my container so that I can later reference it.

To solve this I'm trying a couple of things:

First, I've downloaded a binary headless chromium and I've included it into my API.

File structure:

-run-puppeteer.js    
-headless_shell.tar.gz

Referenced like:

const browser = await puppeteer.launch({
    executablePath: "../headless_shell.tar.gz"
  });

However, I can't import or require it so my lambda doesn't recognize that it exists and doesn't include it in my deployment package. My question here is how do I correctly include the headless file into my API so that I can reference it from within this lambda?

If that isn't an option - I see that I can upload the binary to S3 and then download it on container startup. Any references on where to begin tackling this would be much appreciated.


回答1:


You can use chrome-aws-lambda to either package chrome with your Lambda or create a Lambda Layer to avoid the package size.

I did something similar here based on chrome-aws-lambda




回答2:


chrome-aws-lambda indeed is big ~40MB adding to the deployment package, using Layer could potentially reduce the package size but also could increase the size, because the 250MB unzipped limit includes the layer and the lambda code. If you use chrome-aws-lambda then definitely do NOT use puppeteer, instead use puppeteer-core for a smaller size. I did a very similar setup this hopefully it helps1



来源:https://stackoverflow.com/questions/57408181/how-to-bundle-headless-chromium-module-with-aws-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!