[英]Importing helper functions in Serverless
我想將一個助手 function 導入到 Node.js Lambda ZC1C425268E68385D9F14C14無服務器(C17ZA)中。 我嘗試過使用 Layer 函數,以及無服務器的包裝器模塊- 但到目前為止沒有任何效果。
這是我想與其他 Lambda 功能一起使用的 function - helperFunc.js
:
class HelperClass { ... } //a helper class that I want to use in other functions
module.exports = HelperClass
這是我的 Lambda 功能之一。 它還為 API 網關端點 - users.js
提供服務:
"use strict";
const helperClass = require('./helperFunc') //I don't know how to do this
module.exports.get = (event, context, callback) => {
const params = { ... } //DynamoDB Params
// a bunch of code that uses the helper class wrapper
callback(null, {
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
}, body: JSON.stringify(res), statusCode: 200})
}
};
這就是我的 serverless.yml 目前的樣子:
...
getUsers:
handler: src/users.get
events:
- http:
path: users
method: get
authorizer: authFunc
這就是我的項目目錄的樣子:
./
serverless.yml
./src
helperFunc.js
users.js
./auth
更新 1 :我終於能夠使用 Lambda 層實現此功能。 但是,由於復雜的目錄設置,它仍然感覺好像不是最好的方法。
這是更新的項目目錄:
./
serverless.yml
./layers/helperFunc/nodejs/node_modules/helperFunc
index.js
./src
users.js
./auth
這是更新后的serverless.yml
文件:
layers:
helperFunc:
path: layers/helperFunc
...
functions:
getUsers:
handler: src/users.get
layers:
- {Ref: HelperFuncLambdaLayer} # referencing the layer in cf
events:
- http:
path: users
method: get
authorizer: authFunc
解決方案是使用serverless-webpack-plugin 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.