[英]Firebase Admin Nodejs cannot access a .json file in AWS Lambda layer
[英]AWS Lambda: NodeJs custom layer
我是 nodejs 和 aws lambda 的新手。 我正在尝试为我的一堆 lambda 函数创建具有常用函数的层。 在 lambda 处理程序中,自定义模块的导入具有以下定义: let commonService = require('@common/service');
带有模块文件的 zip 结构如下:
nodejs
--node_modules
--@custom
--service
--index.js
--package.json
但我收到错误: "errorMessage": "Error: Cannot find module '@common/service'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
@custom/service模块 package.json 包含以下内容:
{
"name": "@common/service",
"version": "1.0.0",
"main": "index.js",
"license": "ISC",
"dependencies": {
/***
}
}
提前致谢!
尝试使用AWS Lambda 层。
使用图层有两个主要好处。
node_modules
文件夹层次结构。如何使用它们? 这是一个相当深入的话题,但它的要点是您可以:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: My Serverless App
Parameters:
# ...
Globals:
Function:
Runtime: nodejs10.x
# Environment:
# Variables:
# NODE_ENV: test
Resources:
CommonDependenciesLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: my-common-deps
Description: Common dependencies for my NodeJS apps
ContentUri: ../common-deps/
RetentionPolicy: Retain
Metadata:
BuildMethod: nodejs10.x
performSomeFunction:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: performSomeFunction
Handler: lambda.someFunction
Layers:
# You can either Ref a layer defined in this SAM file like this.
# - !Ref CommonDependenciesLayer
# ... or you can hard-code an external ARN
# - arn:aws:lambda:us-east-1:nnnnnnnnnnnnn:layer:myapp-common-deps:5
CodeUri: ./
# ...
mysql
库放在上面的CommonDependenciesLayer
中。 只要我的 Lambda function 指向 SAM 文件中的层,我所要做的就是在我的代码中使用require('mysql')
。 它会在那里。当你开始工作时,有一些关于 Layer 部署的微妙之处需要考虑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.