sambaiz / puppeteer-lambda-starter-kit

Starter Kit for running Headless-Chrome by Puppeteer on AWS Lambda.
MIT License
580 stars 82 forks source link

Having a bit of trouble getting past browser.newPage() on AWS Lambda #36

Closed nschubach closed 6 years ago

nschubach commented 6 years ago

Using the latest version (1.1.2), I am unable to get past const page = await browser.newPage(); using an uploaded package.zip

I have added some debugging to see what's going on and it doesn't dump anything to the AWS log.

I even went as far as downloading the @latest for every package used and am now trying to use Node 8.10 after changing the Babel version to Node 8.10.

My index starts off with:

const setup = require('./starter-kit/setup');

exports.handler = async (event, context, callback) => {
    context.callbackWaitsForEmptyEventLoop = false;
    const browser = await setup.getBrowser();
    exports.run(browser).then(
        (result) => callback(null, result)
    ).catch(
        (err) => callback(err)
    );
};

exports.run = async (browser) => {
    try {
        console.log('new page');
        const page = await browser.newPage();

        console.log('emulate screen');
...

The Amazon log is:

START RequestId: <guid> Version: $LATEST
2018-08-24T18:38:41.789Z    <guid>  setup local chrome
2018-08-24T18:38:45.726Z    <guid>  setup done
2018-08-24T18:38:47.545Z    <guid>  new page
END RequestId: <guid>
REPORT RequestId: <guid>    Duration: 5821.84 ms    Billed Duration: 5900 ms    Memory Size: 512 MB Max Memory Used: 335 MB 

It's bailing out at const page = await browser.newPage(); and never getting to dump out console.log('emulate screen');. The catch is just a console.log(e) for this try block.

I've attempted to use various versions of chrome and the only variant response I've gotten is with this version: https://github.com/adieuadieu/serverless-chrome/releases/tag/v1.0.0-54

In which the log was:

START RequestId: <guid> Version: $LATEST
2018-08-24T18:34:33.046Z    <guid>  setup local chrome
2018-08-24T18:34:37.184Z    <guid>  setup done
2018-08-24T18:34:37.208Z    <guid>  Error: Failed to launch chrome! spawn /tmp/headless_shell ENOENT

TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

at onClose (/var/task/node_modules/puppeteer/lib/Launcher.js:299:14)
at ChildProcess.helper.addEventListener.error (/var/task/node_modules/puppeteer/lib/Launcher.js:290:64)
...
nschubach commented 6 years ago

Just realized the alternate error I was getting is because the file was not named properly. Extracted the headless-chrome, renamed to headless_shell and re-tarred the file and I got the familiar:

START RequestId: Version: $LATEST
2018-08-24T18:59:26.288Z    setup local chrome
2018-08-24T18:59:30.069Z    setup done
2018-08-24T18:59:31.425Z    new page
END RequestId: 
REPORT RequestId:  Duration: 5201.74 ms Billed Duration: 5300 ms    Memory Size: 512 MB Max Memory Used: 224 MB 
nschubach commented 6 years ago

Also tried:

const launchOptionForLambda = [
    // error when launch(); No usable sandbox! Update your kernel
    '--no-sandbox',
    // error when launch(); Failed to load libosmesa.so
    '--disable-gpu',
    // freeze when newPage()
    '--single-process',
    '--disable-setuid-sandbox',
];
nschubach commented 6 years ago

Doh... applied this change request to my code and it is working...

https://github.com/sambaiz/puppeteer-lambda-starter-kit/pull/26