简体   繁体   中英

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" 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:

This is my 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',
'--disable-background-networking',
'--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-extensions',
'--disable-features=AudioServiceOutOfProcess',
'--disable-hang-monitor', '--disable-ipc-flooding-protection',
'--disable-notifications',
'--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 INFO chromium.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

Have you tried installing a chrome-aws-lambda layer and then adding it to your function? See the instructions here in the "AWS Lambda Layer" section.

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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