我正在尝试在本地运行一个 Node lambda 来调试它。我在 vsCode 中使用 Serverless 和这个启动配置
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/node_modules/.bin/sls",
"args": [
"invoke",
"local",
"--function",
"hello",
"--data",
"hello world"
]
}
]
}
我的 export.handler 看起来像这样:
module.exports.handler = (event, context, callback) => {
if (event.triggerSource === CONSTANTS.TRIGGER_SOURCE) {
console.log("event = " + JSON.stringify(event));
const uri = process.env.SCT_URL_BASE;
const country = process.env.SCT_COUNTRY;
const username =
event.request.userAttributes[CONSTANTS.USER_ATTRIBUTES];
const codeP = event.request.codeParameter;
console.log("URI = " + url);
console.log("Code:" + codeP);
getUrlData(uri, country, username, codeP);
} else {
context.done(null, event);
}
};
当我运行 Debug模式时,它什么也不做。 Serverless 不会抛出任何错误,我只是无法到达函数内部。
另外,还有一点我不明白。在无服务器文档中它说:
--function or -f The name of the function in your service that you want to invoke locally. Required.
我不知道他们指的是什么,是我们调用来运行 lambda 的函数还是调用 lambda 时调用的函数。在这种情况下,我导出的函数是“处理程序”,但它也不起作用。
提前致谢。
最佳答案
我已经使用过这种方法并且它对我有用:
https://standardofnorms.wordpress.com/2017/12/03/locally-debugging-aws-lambdas-written-in-node-js/
糟糕的是,由于无服务器社区更大,我想使用无服务器而不是 lambda-local 包。不过,Lambda-local 工作起来很有魅力,所以我从这里向它的创造者送上一个大大的拥抱。
仍然非常欢迎回答第一个问题。
编辑:好的,我想通了。
结果Serverless作为框架,在我们需要添加一些配置的时候使用了一个serverless.yml文件。在那里,我必须创建要使用无服务器命令运行的函数,然后将其指向我的处理程序所在的文件。这是我现在的 serverles.yml:
service: serverless-simple
frameworkVersion: ">=1.1.0 <2.0.0"
provider:
name: aws
runtime: nodejs4.3
functions:
lambdaHandler:
handler: src/customMessageLambda.handler
events:
- http:
path: ping
当然,我必须对该文件进行更多研究,但我已经解决了我的问题。
希望这有时能对某人有所帮助。
关于javascript - 无服务器调用本地不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50598080/