我正在练习 aws lambda 函数和无服务器 yml。我正在使用打字稿和 javascript es6。我通过离线插件测试我的无服务器 yml 文件。当我导出 lambda 函数并在无服务器 yml 文件中调用它时,如下所示:handler: src/handlers/hello.hello
,我的 lambda 会触发该函数并且它按预期工作。我正在尝试将我的 lambda 函数作为导出默认函数并像这样调用它 handler: src/handlers/hello.default
。我收到错误:hello is not a function
。我不知道我的导出默认值做错了什么。
这个逻辑有效
import { APIGatewayEvent } from "aws-lambda";
export async function hello(event: APIGatewayEvent) {
console.log(event);
try {
return {
statusCode: 200,
body: JSON.stringify("hello emmy"),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error),
};
}
}
此导出默认值不起作用
import { APIGatewayEvent } from "aws-lambda";
export default async function hello(event: APIGatewayEvent) {
try {
return {
statusCode: 200,
body: JSON.stringify("hello world"),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error),
};
}
}
答案 0 :(得分:0)
我认为很多可能归结为您的无服务器配置。我通过以下方式使用 serverless-bundle
作为插件:
plugins:
- serverless-bundle
为了能够在我使用无服务器框架创建的 Lambda 中利用 ES6 和其他现代 javascript 优点。
我像这样创建和导出我的函数:
export default async function (event)
然后在我的 serverless.yml 设置处理程序如下:
handler: handler.default
这让我的所有事情都正常运行。