如何在使用 Amplify 创建的 AWS lambda 函数上使用 chrome-aws-lambda
How to use chrome-aws-lambda on a AWS lambda function created with Amplify
我正在尝试使用 Amplify 在 AWS 中创建此 lambda 函数,但是当我尝试启动 chromium 时,lambda 函数没有 return 任何值,并且该函数最终会计时:
"errorMessage": "2020-12-09T02:56:56.210Z 57402f8e-9fb2-4341-837d-bdf2ee6e9262 Task timed out after 25.57 seconds"
我按照@James Shapiro 的建议添加了层,现在我得到了一个 URL return 用于铬,但它仍然没有启动它:
这是我的功能:
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 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
您是否尝试过安装 chrome-aws-lambda
层,然后将其添加到您的函数中?请参阅“AWS Lambda 层”部分中的说明 here。
我不得不增加 lambda 函数的内存和超时,但是,如果没有 James Shapiro 的帮助,我不认为它会起作用,谢谢。
我正在尝试使用 Amplify 在 AWS 中创建此 lambda 函数,但是当我尝试启动 chromium 时,lambda 函数没有 return 任何值,并且该函数最终会计时:
"errorMessage": "2020-12-09T02:56:56.210Z 57402f8e-9fb2-4341-837d-bdf2ee6e9262 Task timed out after 25.57 seconds"
我按照@James Shapiro 的建议添加了层,现在我得到了一个 URL return 用于铬,但它仍然没有启动它:
这是我的功能:
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 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
您是否尝试过安装 chrome-aws-lambda
层,然后将其添加到您的函数中?请参阅“AWS Lambda 层”部分中的说明 here。
我不得不增加 lambda 函数的内存和超时,但是,如果没有 James Shapiro 的帮助,我不认为它会起作用,谢谢。