node.js - lambda 函数未找到 lambda 层中的 Chrome
问题描述
我开始尝试用 node 和 puppeteer 编写一个 lambda 函数。我正在使用无服务器框架
我一直在尝试遵循https://github.com/alixaxel/chrome-aws-lambda上的指示。我的功能在本地按预期工作,具有:
$ sls invoke local -f hello
按预期工作。但是,当我运行时:
$ sls invoke -f hello
我得到:
$ sls invoke -f hello
{
"errorType": "Error",
"errorMessage": "Cannot find module 'chr'\nRequire stack:\n- /var/task/index.js\n- /var/task/handler.js\n- /var/task/s_hello.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Error: Cannot find module 'chr'",
"Require stack:",
"- /var/task/index.js",
"- /var/task/handler.js",
"- /var/task/s_hello.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)",
" at Module._require.o.require (/var/task/serverless_sdk/index.js:9:72748)",
" at require (internal/modules/cjs/helpers.js:77:18)",
" at Object.main (/var/task/index.js:47:26)",
" at module.exports.hello (/var/task/handler.js:6:25)",
" at Runtime.handler (/var/task/serverless_sdk/index.js:9:137040)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
我怎样才能得到这个工作?
我的 handler.js 包含:
'use strict';
var index = require('./index.js');
module.exports.hello = async event => {
// var t = async event => {
var res = await index.main();
console.log('hello');
console.log(res);
console.log('IN HANDLER');
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'main function executed!',
input: event,
......
我的 index.js 包含:
async function main(event, context, callback) {
const os = require('os');
let result = null;
let browser = null;
if (os.platform=='win32') {
const puppeteer= require('puppeteer-core');
browser = await puppeteer.launch({
executablePath: 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe',
headless: false,
ignoreHTTPSErrors:true
})
} else {
// launch a headless browser
const chromeLambda = require('chrome-aws-lambda');
console.log(os.platform());
console.log('lambda');
browser = await chromeLambda.puppeteer.launch({
args: chromeLambda.args,
executablePath: await chromeLambda.executablePath,
defaultViewport,
headless:true
});
var page = await browser.newPage();
........
};
module.exports.main = main;
包.json:
"license": "ISC",
“依赖项”:{“chrome-aws-lambda”:“^3.1.1”,“puppeteer-core”:“^3.1.0”
}
无服务器.yml:
# Welcome to Serverless!
#
.......
# Happy Coding!
plugins:
- serverless-offline
service: xxxxx
# app and org for use with dashboard.serverless.com
app: yyyyy
org: xxxx
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"
provider:
name: aws
runtime: nodejs12.x
region: us-east-1
# here we put the layers we want to use
layers:
# Google Chrome for AWS Lambda as a layer
# Make sure you use the latest version depending on the region
# https://github.com/shelfio/chrome-aws-lambda-layer
- arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:10
- arn:aws:lambda:us-east-1:155754363046:layer:chr:1
.....
functions:
hello:
handler: handler.hello
# main:
# handler: handler.main
# The following are a few example events you can configure
# NOTE: Please make sure to change your handler code to work with those events
# Check the event documentation for details
events:
- http:
path: hello/get
method: get
.....
解决方案
推荐阅读
- mysql - 替换 SQL 表中的值
- finite-automata - 设计一个 FA 来接受 L,其中 L = 'a' 总是出现三倍的字符串
- c# - 使用 observable 按字母顺序排序数据
- java - NPE 很难从 logcat 中发现它似乎是 Arraylist 但应该设置每个项目
- docker - Docker 拒绝挂载 NFS 共享
- sql - Laravel Eloquent:选择一个字段等于另一个字段的记录
- google-apps-script - 继续接收 抱歉,无法删除所有未冻结的行
- angular - angular: 封装一个 Stepper 组件
- python - Python:为什么 for 循环在 join() 函数的中间?
- java - Spring WebClient - 如何处理错误场景