Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Called a lambda function once, it's executed twice

This is more of a concern than a question, but still, has anyone experienced this before? Does anyone know how to prevent it?

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.

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`);
    },
};

In CloudWatch I can see one Calling .... and two L2 called!

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?

like image 786
Mehran Avatar asked Aug 20 '19 01:08

Mehran


Video Answer


1 Answers

Your lambda function must be idempotent, because it can be called twice in different situations.

https://aws.amazon.com/premiumsupport/knowledge-center/lambda-function-idempotent/

https://cloudonaut.io/your-lambda-function-might-execute-twice-deal-with-it/

like image 178
Azize Avatar answered Sep 28 '22 07:09

Azize