Kri*_*sna 2 amazon-web-services aws-lambda aws-serverless
我正在练习 aws lambda 函数和无服务器 yml。我正在使用 typescript 和 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),
};
}
}
Run Code Online (Sandbox Code Playgroud)
此导出默认不起作用
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),
};
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这很大程度上可能取决于您的无服务器配置。我通过以下方式用作serverless-bundle插件:
plugins:
- serverless-bundle
Run Code Online (Sandbox Code Playgroud)
为了能够在使用无服务器框架创建的 Lambda 中利用 ES6 和其他现代 JavaScript 功能。
我像这样创建并导出我的函数:
export default async function (event)
Run Code Online (Sandbox Code Playgroud)
然后在我的 serverless.yml 设置处理程序如下:
handler: handler.default
Run Code Online (Sandbox Code Playgroud)
这让我的一切都正常运行。
| 归档时间: |
|
| 查看次数: |
2025 次 |
| 最近记录: |