问题描述
这不是问题,而是更多的问题,但是,以前有人遇到过吗?有人知道如何预防吗?
This is more of a concern than a question, but still, has anyone experienced this before? Does anyone know how to prevent it?
我有一个lambda函数(L1),它调用了所有用NodeJs编写的第二个lambda函数(L2)(运行时:Node.Js 8.10,而aws-sdk
应该是v2.488.0-但我只是将其粘贴到文档).简而言之,L1应该调用L2,并且在执行L2时执行两次!我通过将日志写入CloudWatch发现了这一点,并且可以看到一个L1日志和两个L2日志.
I have a lambda function (L1) which calls a second lambda function (L2) all written in NodeJs (runtime: Node.Js 8.10, and aws-sdk
should be v2.488.0 - but I'm just pasting that from the documentation). The short story is that L1 is supposed to call L2, and when it does L2 is executed twice! I discovered this by writing logs to CloudWatch and I could see one L1 log and two L2 logs.
这是L1和L2的简化版本.
Here's a simplified version of L1 and L2.
L1:
const AWS = require('aws-sdk');
const lambda = new AWS.Lambda();
module.exports = {
handler: async (event, context, callback) => {
const lambdaParams = {
FunctionName: 'L2',
Qualifier: `dev`,
Payload: JSON.stringify({}),
};
console.log(`Calling: ${JSON.stringify(lambdaParams)}`);
return await lambda.invoke(lambdaParams).promise();
},
};
L2:
module.exports = {
handler: async (event, context, callback) => {
console.log(`L2 called`);
},
};
在CloudWatch中,我可以看到一个Calling ....
和两个L2 called
!
In CloudWatch I can see one Calling ....
and two L2 called
!
顺便说一句,这并非一直都发生.这是步骤函数过程的一部分,该过程将调用L1 10k次.我的代码的编写方式是,如果L2执行两次(每个调用一次),它将使整个过程失败(因为L2仅在记录不存在时才向DB插入记录,而在记录不存在时会失败).到目前为止,我设法记录了3次此行为.它们全部处理相同的10k项,每次都面对不同迭代的问题.
BTW, this does not happen all the time. This is part of a step function process which was going to call L1 10k times. My code is written in a way that if L2 is executed twice (per one call), it will fail the whole process (because L2 inserts a record to DB only if it does not exist and fails if it does). So far, I managed to log this behaviour three times. All of them processing the same 10k items, facing the issue at a different iteration each time.
有人有相同的经历吗?甚至更好的是,知道如何确保一个调用导致一个执行?
Does anyone have the same experience? Or even better, knows how to make sure one call leads to exactly one execution?
推荐答案
您的lambda函数必须是幂等的,因为在不同情况下可以调用两次.
Your lambda function must be idempotent, because it can be called twice in different situations.
https://aws.amazon.com/premiumsupport/知识中心/lambda-功能-幂等/
https://cloudonaut.io/您的lambda功能可能会执行两次与它的交易/
这篇关于调用一次lambda函数,执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!