3

I configured AWS S3 trigger to lambda function with PUT operation.

Every 2 minutes uploading .txt file of size 100kb.

Sometimes S3 will trigger lambda twice with same event and time.

Trigger 1 :

START RequestId: f32a3353-bd53-48fd-aa49-c09ee9c82a3e 2019-07-30T22:47:05.122Z

Trigger 2 :

START RequestId: f32a3353-bd53-48fd-aa49-c09ee9c82a3e 2019-07-30T22:47:05.98Z

How can i avoid this?

abhimanyu
  • 730
  • 1
  • 10
  • 23
  • Avoid unhandled exceptions (including timeouts) if you want to avoid this behaviour. – 404 Jul 31 '19 at 08:45
  • Checking and increasing timeout may solved this issue; See this issue https://stackoverflow.com/questions/32064038/aws-lambda-function-triggering-multiple-times-for-a-single-event – MuntingInsekto Jul 31 '19 at 08:50
  • 1
    Possible duplicate of [aws lambda function triggering multiple times for a single event](https://stackoverflow.com/questions/32064038/aws-lambda-function-triggering-multiple-times-for-a-single-event) – MuntingInsekto Jul 31 '19 at 08:51
  • thank you @vnpnlz. I particularly mentioned time. in your case different timing. – abhimanyu Jul 31 '19 at 09:52

2 Answers2

4

Let me quote the documentation:

Occasionally, your function may receive the same event multiple times, even if no error occurs.

Another one:

When an error occurs, your function may be invoked multiple times. Retry behavior varies by error type, client, event source, and invocation type. For example, if you invoke a function asynchronously and it returns an error, Lambda executes the function up to two more times.

Reference: https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html

Idempotent

Your lambda must be idempotent to handle this behavior, see more detail on links below:

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

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

Azize
  • 4,006
  • 2
  • 22
  • 38
  • this is a really generalized issue that seems to be bigger than just the s3 triggers, i've seen it with s3 uploads, cron jobs, asynchronous programmatic invocations, and api gateway triggers. the timeout change suggested as an accepted answer in this question https://stackoverflow.com/questions/32064038/aws-lambda-function-triggering-multiple-times-for-a-single-event isn't sufficient for all use cases. @Azize i wish this answer was on the other question as well as i think it should be elevated to common knowledge when developing with Lambdas. wish i could upvote more! – jcp Mar 29 '21 at 21:21
  • and a seemingly standard implementation for this from aws: https://awslabs.github.io/aws-lambda-powertools-python/api/utilities/idempotency/index.html – jcp Mar 29 '21 at 22:57
0

It's probably a problem with the invocation of the Lambda function, see https://docs.aws.amazon.com/lambda/latest/dg/retries-on-errors.html

As such it will be harmless (the code isn't running twice)

Vorsprung
  • 32,923
  • 5
  • 39
  • 63
  • thank you @Vorspung. i will consider invocation error. Account – The maximum number of function instances are already running, or requests are being made too quickly. how this maximum instances are running, instances will create based on memory size. Anyway function is executing twice. – abhimanyu Jul 31 '19 at 08:50