aws lambda function triggering multiple times for a single event

前端 未结 4 1211
北恋
北恋 2020-12-13 18:19

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

4条回答
  •  旧巷少年郎
    2020-12-13 18:59

    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!

提交回复
热议问题