Node.js lambda函数未找到lambda层中的铬
我开始尝试用node和puppeter编写lambda函数。我使用的是无服务器框架 我一直在努力按指示行事。我的功能在本地按预期工作,包括:Node.js lambda函数未找到lambda层中的铬,node.js,aws-lambda,serverless-framework,Node.js,Aws Lambda,Serverless Framework,我开始尝试用node和puppeter编写lambda函数。我使用的是无服务器框架 我一直在努力按指示行事。我的功能在本地按预期工作,包括: $ sls invoke local -f hello 按预期工作。然而,当我跑步时: $ sls invoke -f hello 我得到: $ sls invoke -f hello { "errorType": "Error", "errorMessage": "Cannot find module 'chr'\nRequire stack:\n
$ 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)"
]
我怎样才能让它工作
My 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,
......
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;
my index.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,
......
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;
package.json:
"license": "ISC",
“依赖项”:{
“chrome aws lambda”:“^3.1.1”,
“木偶核心”:“^3.1.0”
serverless.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
.....
您是否为您的图层创建了“适当”的文件夹结构?例如
nodejs/nodel_modules/chrome aws lambda
?@Ersoy,我不明白您所说的“适当”是什么意思。lambda图层有一些约定,文件夹的名称应遵循此约定来注入/使用图层您创建了“适当”吗你的图层的文件夹结构?例如nodejs/nodel_modules/chrome aws lambda
?@Ersoy,我不明白你所说的“正确”是什么意思。lambda图层有一些约定,文件夹的名称应该遵循这个约定来注入/使用图层