簡體   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:

"errorMessage": "2020-12-09T02:56:56.210Z 57402f8e-9fb2-4341-837d-bdf2ee6e9262 Task timed out after 25.57 seconds"我按照@James Shapiro 的建議添加了圖層,現在我得到了 ZE6B391A8D2C4D45902A23A8B65857 返回對於鉻,但它仍然沒有啟動它:

這是我的 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
        })
    }

}

這是依賴項:

{
  "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"
  }
}

日志:

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-timer-throttling',
'--disable-backgrounding-occluded-windows', '--disable-breakpad',
'--disable-client-side-phishing-detection',
'--disable-component-update', '--disable-default-apps',
'--disable-dev-shm-usage', '--disable-domain-reliability',
'--禁用擴展',
'--disable-features=AudioServiceOutOfProcess',
'--disable-hang-monitor', '--disable-ipc-flooding-protection',
'--禁用通知',
'--disable-offer-store-unmasked-wallet-cards',
'--disable-popup-blocking', '--disable-print-preview',
'--disable-prompt-on-repost', '--disable-renderer-backgrounding',
'--disable-setuid-sandbox', '--disable-speech-api',
'--disable-sync', '--disk-cache-size=33554432',
'--hide-scrollbars', '--ignore-gpu-blacklist',
'--metrics-recording-only', '--mute-audio',
'--no-default-browser-check', '--no-first-run', '--no-pings',
'--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 信息鉻。 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-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 任務超時秒后。

您是否嘗試過安裝chrome-aws-lambda層,然后將其添加到 function 中? 請參閱“AWS Lambda 層”部分中的說明

我不得不增加 memory 和超時 lambda function,但是,我不認為它會工作沒有詹姆斯夏皮羅的幫助,

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM