adieuadieu / serverless-chrome

🌐 Run headless Chrome/Chromium on AWS Lambda
MIT License
2.87k stars 282 forks source link

Error occured in serverless-plugin-chrome wrapper when trying to ensure Chrome for handler() #249

Closed evgeniyannenkov closed 4 years ago

evgeniyannenkov commented 4 years ago

Hi,

I've got the next error with serverless-chrome-plugin

ERROR Error occured in serverless-plugin-chrome wrapper when trying to ensure Chrome for handler() handler. { functions: [ 'processsqsevent' ], flags: [ '--window-size=1280,1696', '--hide-scrollbars' ], chromePath: '/var/task/headless-chromium' } Error: Unable to start Chrome. If you have the DEBUG env variable set,there will be more in the logs. at /var/task/api/process_sqs.js:257:5970 at Generator.throw (<anonymous>) at n (/var/task/api/process_sqs.js:257:5007) at /var/task/api/process_sqs.js:257:5129 at processTicksAndRejections (internal/process/task_queues.js:93:5)

Debug mode shows the next errors:

INFO @serverless-chrome/lambda: Error trying to spawn chrome: Error: connect ECONNREFUSED 127.0.0.1:9222 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) { errno: 'ECONNREFUSED', code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 9222 } INFO @serverless-chrome/lambda: stderr log: /var/task/headless-chromium: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory Node version 12.13.1

Any thoughts? Could anybody help?

jlemoscds commented 4 years ago

Here I'm in the same situation. Any news?

NikitaSyrovatnikov commented 4 years ago

The same issue

evgeniyannenkov commented 4 years ago

Hi there!

Based on the fact that I did not find the answer to my question, the fastest way to restore broken functionality in my project is to migrate to chrome-aws-lambda.

That's my way:

1) Add chrome-aws-lambda to the project; 2) Add this package to webpack externals;

module.exports = {
    externals : ['chrome-aws-lambda'],
};

3) Create infrastructure to build chrome artifacts (I've used this solution https://github.com/devasur/chrome-aws-lambda-layer);

build_chrome_layer.sh

#!/bin/bash
set -e
rm -rf chrome-aws-lambda
git clone --depth=1 https://github.com/alixaxel/chrome-aws-lambda.git
cd chrome-aws-lambda
make chrome_aws_lambda.zip
ls -lah chrome_aws_lambda.zip
echo -e "\033[33mLayer created successfully...\033[0m"

copy_chrome_artifact.sh

#!/bin/bash
set -e
mv chrome-aws-lambda/chrome_aws_lambda.zip .serverless/chrome_aws_lambda.zip
rm -rf chrome-aws-lambda
echo -e "\033[33mArtifact copied successfully...\033[0m"

4) I've used the serverless-scriptable-plugin to execute my scripts during the build; serverless.yaml

custom                  :
  webpack               :
    webpackConfig       : 'webpack.config.js'
  scriptHooks           :
    before:package:createDeploymentArtifacts:
      - scripts/build_chrome_layer.sh
    after:package:createDeploymentArtifacts:
      - scripts/copy_chrome_artifact.sh

5) Also we should add chrome artifact as a layer to our executable lambda function; serverless.yaml

layers                  :
  chromePreBuilt        :
    package             :
      artifact          : .serverless/chrome_aws_lambda.zip

functions               :
  makeScreenshot       :
    layers              :
      - { Ref: ChromePreBuiltLambdaLayer}
    handler             : api/makeScreenshot.handler

5) Then you can use the library to launch chrome the next way:

const chromium = require('chrome-aws-lambda');

const args = [
    '--remote-debugging-port=9222',
    '--window-size=1280,1696'
];

module.exports = async () => {
    try {
        const executablePath = await chromium.executablePath;
        const options = {
            args            : chromium.args.concat(args),
            defaultViewport : chromium.defaultViewport,
            headless        : chromium.headless,
            executablePath
        };
        return chromium.puppeteer.launch(options);
    } catch (error) {
        console.error('Error launching Chrome: ', error);
        return error;
    }
};

That's it. Hope it will be helpful.

godrose commented 4 years ago

@evgeniyannenkov Hi. I'm trying to use your approach and somehow I'm hitting the lambda size limitation. I'm not using webpack, though. Do you have any idea?

An error occurred: HelloLambdaFunction - Function code combined with layers exceeds the maximum allowed size of 262144000 bytes. The actual size is 299245738 bytes
evgeniyannenkov commented 4 years ago

@godrose Morning! This error is due the fact that you are trying to install a lot of dependencies. Please, check the lambda layers documentation to avoid this issue. https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

godrose commented 4 years ago

@godrose Morning! This error is due the fact that you are trying to install a lot of dependencies. Please, check the lambda layers documentation to avoid this issue. https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

Yeah. I finally made it work after some coffee and head scratching. Thanks for your effort and the detailed description. It has been most valuable to the solution .