nodejs lambda 异步句柄在等待函数后未完成



我正在创建一个与documentdb连接的简单lambda函数。

serverless.yml

# ...
functions:
helloWorld:
handler: src/handlers/helloWorld.handler
events:
- httpApi:
path: /
method: get

src\handlers\helloWorld.js

"use strict";
const { mongoClient } = require("../helpers/dbHelper");
module.exports.handler = async (event, context) => {
try {
const db = await mongoClient();
const data = await db.collection("mytable").find({}).toArray();
console.log(data);
return {
statusCode: 200,
body: JSON.stringify({ message: "hello world" }),
};
} catch (error) {
console.log(error.message);
throw error;
}
};

data变量被正确查询,返回函数按预期工作,但函数直到超时才完成。

如果我把硬编码的返回放在mongoClient()之上,函数将正常完成。

发现它错过了vpc配置

provider:
# ...
vpc:
securityGroupIds:
- sg-xxxxxxxxxxxxx
subnetIds:
- subnet-xxxxxxxxxxx
- subnet-xxxxxxxxxx
- subnet-xxxxxxxxxxxxx

最新更新