当 运行 Chromeless on AWS Lambda 时得到一个空网页

Get an empty webpage when running Chromeless on AWS Lambda

我想在 AWS Lambda 函数上使用 Node.js 包 Chromeless。 我使用两个包:chromless 和 serverless-chrome 在我的本地机器上,我的测试脚本运行良好。当我将它部署到 Lambda 时,我得到 .html() 的空结果 结果不是空字符串,它是一个空页面。 () CloudWatch Logs 中没有错误。 chrome 似乎 运行 没问题,但无法加载网站。 NodeJS 版本是 8.10,async/await 似乎可以工作。 希望有人有想法。

代码:

const launchChrome = require('@serverless-chrome/lambda');
const { Chromeless } = require('chromeless');

let index = async function handler () {

    await launchChrome({
        port: 9222,
        chromeFlags: [
            '--window-size=1200,800',
            '--disable-gpu',
            '--headless'
        ]
    })
    .then(async (chrome) =>
    {

        const chromeless = new Chromeless(
            {
                launchChrome:false,
                cdp:{host: 'localhost', port: 9222, secure: false}
            }
        );

        const html = await chromeless
            .goto('http://www.google.com')
            .wait(5000)
            .html();
        console.log(html);
        chromeless.end();

    })
};

exports.handler = index;

日志:

16:39:44 START RequestId: xxx Version: $LATEST
16:39:52 2018-04-05T16:39:52.163Z xxx   <html><head></head><body></body></html>
16:39:52 END RequestId: xxx
16:39:52 REPORT RequestId: xxx
Duration: 7375.83 ms    Billed Duration: 7400 ms Memory Size: 576 MB

这个问题已经在 Chromeless 问题 [https://github.com/graphcool/chromeless/issues/414] 中得到很好的记录。

无服务器的依赖项中存在导致问题的问题。

要更正它,请更新您的 serverless/package.json 以将 "serverless-plugin-chrome" 与版本“1.0.0-38”挂钩。

例如,我的开发依赖如下所示:

    "devDependencies": {
    "@types/cuid": "^1.3.0",
    "@types/node": "^9.6.2",
    "serverless": "^1.19.0",
    "serverless-offline": "^3.15.3",
    "serverless-plugin-chrome": "1.0.0-38",
    "serverless-plugin-typescript": "^1.0.0"
  }