简体   繁体   English

如何在使用 Amplify 创建的 AWS lambda function 上使用 chrome-aws-lambda

[英]How to use chrome-aws-lambda on a AWS lambda function created with Amplify

I'm trying to create this lambda function in AWS using Amplify however when I try to launch the chromium the lambda function does not return any value and the function end up timing up: I'm trying to create this lambda function in AWS using Amplify however when I try to launch the chromium the lambda function does not return any value and the function end up timing up:

"errorMessage": "2020-12-09T02:56:56.210Z 57402f8e-9fb2-4341-837d-bdf2ee6e9262 Task timed out after 25.57 seconds" I add the Layer as suggested by @James Shapiro, now I'm getting a URL return for the chromium but it still not launching it: "errorMessage": "2020-12-09T02:56:56.210Z 57402f8e-9fb2-4341-837d-bdf2ee6e9262 Task timed out after 25.57 seconds"我按照@James Shapiro 的建议添加了图层,现在我得到了 ZE6B391A8D2C4D45902A23A8B65857 返回对于铬,但它仍然没有启动它:

This is my function:这是我的 function:

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

exports.handler = async (event, context) => {

    const pageToScreenshot = "https://www.google.com"

    console.log('page:', pageToScreenshot);

    if (!pageToScreenshot) return {
        statusCode: 400,
        body: JSON.stringify({ message: 'Page URL not defined' })
    }

    console.log('launch:');
    console.log('chromium.args:', chromium.args);
    console.log('chromium.defaultViewport:', chromium.defaultViewport);
    console.log('chromium.headless:', chromium.headless);

    const executablePath = await chromium.executablePath;

    const browser = await chromium.puppeteer.launch({
        args: chromium.args,
        defaultViewport: chromium.defaultViewport,
        executablePath,
        headless: chromium.headless,
    });
    console.log('page:');

    const page = await browser.newPage();
    console.log('goto:');

    await page.goto(pageToScreenshot, { waitUntil: 'networkidle2' });
    console.log('screenshot:');

    const screenshot = await page.screenshot({ encoding: 'binary' });
    console.log('close:');

    await browser.close();
    console.log('response:');

    return {
        statusCode: 200,
        body: JSON.stringify({
            message: `Complete screenshot of ${pageToScreenshot}`,
            buffer: screenshot
        })
    }

}

This is the dependencies:这是依赖项:

{
  "name": "snapshot31baa866",
  "version": "2.0.0",
  "description": "Lambda function generated by Amplify",
  "main": "index.js",
  "license": "Apache-2.0",
  "dependencies": {
    "chrome-aws-lambda": "^5.5.0",
    "puppeteer-core": "^5.5.0"
  }
}

The logs:日志:

START RequestId: 6dc39c75-0577-492d-b305-6d5c95778eea Version: $LATEST 2020-12-09T04:56:34.141Z 6dc39c75-0577-492d-b305-6d5c95778eea INFO page: https://www.google.com 2020-12-09T04:56:34.141Z 6dc39c75-0577-492d-b305-6d5c95778eea INFO launch: 2020-12-09T04:56:34.143Z 6dc39c75-0577-492d-b305-6d5c95778eea INFO chromium.args: [ '--autoplay-policy=user-gesture-required', START RequestId: 6dc39c75-0577-492d-b305-6d5c95778eea Version: $LATEST 2020-12-09T04:56:34.141Z 6dc39c75-0577-492d-b305-6d5c95778eea INFO page: https://www.google.com 2020-12 -09T04:56:34.141Z 6dc39c75-0577-492d-b305-6d5c95778eea 信息发布:2020-12-09T04:56:34.143Z 6dc39c75-0577-492d-b305-6d5c95778eea 信息 chromium-policy.args: [ =用户手势要求',
'--disable-background-networking', '--禁用后台网络',
'--disable-background-timer-throttling', '--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows', '--disable-breakpad', '--disable-backgrounding-occluded-windows', '--disable-breakpad',
'--disable-client-side-phishing-detection', '--disable-client-side-phishing-detection',
'--disable-component-update', '--disable-default-apps', '--disable-component-update', '--disable-default-apps',
'--disable-dev-shm-usage', '--disable-domain-reliability', '--disable-dev-shm-usage', '--disable-domain-reliability',
'--disable-extensions', '--禁用扩展',
'--disable-features=AudioServiceOutOfProcess', '--disable-features=AudioServiceOutOfProcess',
'--disable-hang-monitor', '--disable-ipc-flooding-protection', '--disable-hang-monitor', '--disable-ipc-flooding-protection',
'--disable-notifications', '--禁用通知',
'--disable-offer-store-unmasked-wallet-cards', '--disable-offer-store-unmasked-wallet-cards',
'--disable-popup-blocking', '--disable-print-preview', '--disable-popup-blocking', '--disable-print-preview',
'--disable-prompt-on-repost', '--disable-renderer-backgrounding', '--disable-prompt-on-repost', '--disable-renderer-backgrounding',
'--disable-setuid-sandbox', '--disable-speech-api', '--disable-setuid-sandbox', '--disable-speech-api',
'--disable-sync', '--disk-cache-size=33554432', '--disable-sync', '--disk-cache-size=33554432',
'--hide-scrollbars', '--ignore-gpu-blacklist', '--hide-scrollbars', '--ignore-gpu-blacklist',
'--metrics-recording-only', '--mute-audio', '--metrics-recording-only', '--mute-audio',
'--no-default-browser-check', '--no-first-run', '--no-pings', '--no-default-browser-check', '--no-first-run', '--no-pings',
'--no-sandbox', '--no-zygote', '--password-store=basic', '--no-sandbox', '--no-zygote', '--password-store=basic',
'--use-gl=swiftshader', '--use-mock-keychain', '--single-process' ] 2020-12-09T04:56:34.168Z 6dc39c75-0577-492d-b305-6d5c95778eea INFO chromium.defaultViewport: { deviceScaleFactor: 1, hasTouch: false, height: 1080, '--use-gl=swiftshader'、'--use-mock-keychain'、'--single-process'] 2020-12-09T04:56:34.168Z 6dc39c75-0577-492d-b305-6d5c95778eea 信息铬。 defaultViewport: { deviceScaleFactor: 1, hasTouch: false, height: 1080,
isLandscape: true, isMobile: false, width: 1920 } 2020-12-09T04:56:34.168Z 6dc39c75-0577-492d-b305-6d5c95778eea INFO chromium.headless: true 2020-12-09T04:56:57.868Z 6dc39c75-0577-492d-b305-6d5c95778eea INFO chromium.executablePath: /tmp/chromium END RequestId: 6dc39c75-0577-492d-b305-6d5c95778eea REPORT RequestId: 6dc39c75-0577-492d-b305-6d5c95778eea Duration: 25009.76 ms Billed Duration: 25000 ms Memory Size: 128 MB Max Memory Used: 128 MB Init Duration: 168.98 ms 2020-12-09T04:56:59.150Z 6dc39c75-0577-492d-b305-6d5c95778eea Task timed out after 25.01 seconds isLandscape: true, isMobile: false, width: 1920 } 2020-12-09T04:56:34.168Z 6dc39c75-0577-492d-b305-6d5c95778eea INFO chromium.headless: true 2020-12-09T04:56:57.868Z 6dc39c75-05 -492d-b305-6d5c95778eea INFO chromium.executablePath: /tmp/chromium END RequestId: 6dc39c75-0577-492d-b305-6d5c95778eea REPORT RequestId: 6dc39c75-0577-492d-b305-6d5c95778eea Duration: 25009.76 ms Billed Duration: 25000 ms Memory Size :最大 128 MB Memory 已使用:128 MB 初始化持续时间:168.98 ms 2020-12-09T04:56:59.150Z 6dc39c75-0577-492d-b305-6d5c95778eea 任务超时秒后。

Have you tried installing a chrome-aws-lambda layer and then adding it to your function?您是否尝试过安装chrome-aws-lambda层,然后将其添加到 function 中? See the instructions here in the "AWS Lambda Layer" section.请参阅“AWS Lambda 层”部分中的说明

I had to increase the memory and time out of the lambda function, however, I don't think it would work without James Shapiro's help, thanks.我不得不增加 memory 和超时 lambda function,但是,我不认为它会工作没有詹姆斯夏皮罗的帮助,

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM