我正在尝试使用NodeJS从另一个lambda函数调用异步lambda函数。最后,我将具有3 lambda函数。 1.功能会询问用户列表 2.功能将询问每个用户的详细信息 3.在数据库中为该用户创建一条记录
第一个请求运行正常,我可以获取用户列表,但看不到任何迹象表明已调用第二个lambda。
'use strict';
const AWS = require("aws-sdk");
const rp = require('request-promise');
const lambda = new AWS.Lambda({
region: "eu-central-1"
});
module.exports.getUserDetailsFromCrowd = async event => {
console.log("Get User Details Function")
var crowdUserDetailsOptions = {
"method": "GET",
"uri": event,
'auth': {
'user': process.env.user,
'pass': process.env.pass,
'sendImmediately': false
},
"json": true,
"headers": {
"Accept": "application/json"
}
}
return rp(crowdUserDetailsOptions).then(function (result) {
console.log(JSON.stringify(result))
}).catch(function (err) {
console.log(JSON.stringify(err))
})
};
module.exports.getUserListFromCrowd = async event => {
console.log("GetUserListFunction")
var crowdUserGroupOptions = {
"method": "GET",
"uri": process.env.uri,
'auth': {
'user': process.env.user,
'pass': process.env.pass,
'sendImmediately': false
},
"json": true,
"headers": {
"Accept": "application/json"
}
};
return rp(crowdUserGroupOptions)
.then(function (body) {
console.log("Request Sent")
const userList = body;
var userLinks = userList.users;
for (var i = 0; i < userLinks.length; i++) {
var requestLink = userLinks[i].link.href + "&expand=attributes"
console.log("Request: " + i)
console.log(requestLink)
const params = {
FunctionName: "applicationName-stageName-getUserDetailsFromCrowd",
InvocationType: "Event",
Payload: requestLink,
LogType: "Tail",
}
console.log("Calling Lambda")
lambda.invoke(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
console.log("Called Lambda")
}
})
.catch(function (err) {
console.log(JSON.stringify(err));
});
};
我得到了以下日志:
2019-11-08T08:38:29.892Z 1da97723-f792-408a-b6dd-24c7d1b343d4 INFO Request: 9
2019-11-08T08:38:29.892Z 1da97723-f792-408a-b6dd-24c7d1b343d4 INFO https://aaa.com/user/details
2019-11-08T08:38:29.892Z 1da97723-f792-408a-b6dd-24c7d1b343d4 INFO Calling Lambda
2019-11-08T08:38:29.894Z 1da97723-f792-408a-b6dd-24c7d1b343d4 INFO Called Lambda
我至少期望在“ Calling Lambda”和“ Called Lambda”之间看到一些东西,但是什么也没有发生。另外,我在控制台上看不到任何第二个功能已被调用的日志。
答案 0 :(得分:2)
Lambda将被触发,问题是您不等待调用完成。您的Lambda是async
,但是您实际上并没有使用await
,并且由于lambda.invoke
是一个I / O绑定调用,因此您的代码中没有什么会迫使Lambda等待完成每次调用。
这是一种使内部呼叫排队并让Lambda等待结果的方式:
module.exports.getUserListFromCrowd = async event => {
console.log('GetUserListFunction')
const crowdUserGroupOptions = {
method: 'GET',
uri: process.env.uri,
auth: {
user: process.env.user,
pass: process.env.pass,
sendImmediately: false
},
json: true,
headers: {
Accept: 'application/json'
}
};
const { users } = await rp(crowdUserGroupOptions);
console.log('Request Sent')
const requests = users.map(u =>
new Promise((resolve, reject) => {
const url = `${u.link.href}&expand=attributes`;
const params = {
FunctionName: 'applicationName-stageName-getUserDetailsFromCrowd',
InvocationType: 'Event',
Payload: requestLink,
LogType: 'Tail',
}
console.log('Calling Lambda');
lambda.invoke(params, (err, data) => err ? reject(err) : resolve(data));
})
);
console.log(`Waiting for ${requests.length} requests to finish...`);
const results = await Promise.all(requests);
console.log('Lambda results');
results.forEach((x, i) => console.log(`Request ${i+1} returned ${x}`));
};