[英]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.