Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws lambda function triggering multiple times for a single event

I am using aws lambda function to convert uploaded wav file in a bucket to mp3 format and later move file to another bucket. It is working correctly. But there's a problem with triggering. When i upload small wav files,lambda function is called once. But when i upload a large sized wav file, this function is triggered multiple times.

I have googled this issue and found that it is stateless, so it will be called multiple times(not sure this trigger is for multiple upload or a same upload).

https://aws.amazon.com/lambda/faqs/

Is there any method to call this function once for a single upload?

like image 505
Ijas Ahamed N Avatar asked Aug 18 '15 05:08

Ijas Ahamed N


People also ask

Can one Lambda function have multiple triggers?

Your function can have multiple triggers. Each trigger acts as a client invoking your function independently. Each event that Lambda passes to your function has data from only one client or trigger.

How many triggers can a Lambda function have?

At the time that you create a Lambda function, you can specify only one trigger.

How does Lambda handle multiple requests?

AWS Lambda is capable of serving multiple requests by horizontally scaling for multiple containers. Lambda can support up to 1000 parallel container executions by default. there are 1000 requests in 10 secs to the API. How many containers will be created and how many threads.


3 Answers

Short version: Try increasing timeout setting in your lambda function configuration.

Long version:

I guess you are running into the lambda function being timed out here.

S3 events are asynchronous in nature and lambda function listening to S3 events is retried atleast 3 times before that event is rejected. You mentioned your lambda function is executed only once (with no error) during smaller sized upload upon which you do conversion and re-upload. There is a possibility that the time required for conversion and re-upload from your code is greater than the timeout setting of your lambda function.

Therefore, you might want to try increasing the timeout setting in your lambda function configuration.

By the way, one way to confirm that your lambda function is invoked multiple times is to look into cloudwatch logs for the event id (67fe6073-e19c-11e5-1111-6bqw43hkbea3) occurrence -

START RequestId: 67jh48x4-abcd-11e5-1111-6bqw43hkbea3 Version: $LATEST 

This event id represents a specific event for which lambda was invoked and should be same for all lambda executions that are responsible for the same S3 event.

Also, you can look for execution time (Duration) in the following log line that marks end of one lambda execution -

REPORT RequestId: 67jh48x4-abcd-11e5-1111-6bqw43hkbea3  Duration: 244.10 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 20 MB 

If not a solution, it will at least give you some room to debug in right direction. Let me know how it goes.

like image 100
rk2 Avatar answered Sep 20 '22 13:09

rk2


Any event Executing Lambda several times is due to retry behavior of Lambda as specified in AWS document.

Your code might raise an exception, time out, or run out of memory. The runtime executing your code might encounter an error and stop. You might run out concurrency and be throttled.

There could be some error in Lambda which makes the client or service invoking the Lambda function to retry.

Use CloudWatch logs to find the error and resolving it could resolve the problem.

I too faced the same problem, in my case it's because of application error, resolving it helped me.

Recently AWS Lambda has new property to change the default Retry nature. Set the Retry attempts to 0 (default 2) under Asynchronous invocation settings.

like image 23
Tarun Mootala Avatar answered Sep 17 '22 13:09

Tarun Mootala


The context object contains information on which request ID you are currently handling. This ID won't change even if the same event fires multiple times. You could save this ID for every time an event triggers and then check that the last ID you handled isn't the same as the current one.

Here's my final code to fix this issue (NodeJS with MongooseJS database handler):

exports.handler = function(event, context, lambdaCallback) {        
    Events.findOneAndUpdate(
        { name: 'some-event-name' }, 
        { lastRequestId: context.awsRequestId }).then(function(event) {
        
        if(event.lastRequestId == context.awsRequestId) {
            return;
        }

        /* Run the actual job */
        ...
    });
}

Hope this helps!

like image 32
maxpaj Avatar answered Sep 20 '22 13:09

maxpaj