Here’s the configuration on my serverless.yml
for the lambda:
sendXXX:
handler: functions/sendXXX/index.handler
timeout: 55
provisionedConcurrency: 1
events:
- schedule: rate(1 minute)
Here’s the relevant Cloudwatch log:
2024-06-25T11:50:32.134Z START RequestId: c2cfe6d6-10d3-4520-8ef1-c82730103331 Version: 170
2024-06-25T11:50:35.698Z START RequestId: 3e331a19-31d9-4342-af21-fe0e7e023c6e Version: 170
2024-06-25T11:50:37.443Z END RequestId: c2cfe6d6-10d3-4520-8ef1-c82730103331
2024-06-25T11:50:37.443Z REPORT RequestId: c2cfe6d6-10d3-4520-8ef1-c82730103331 Duration: 5309.22 ms Billed Duration: 5310 ms Memory Size: 512 MB Max Memory Used: 328 MB
2024-06-25T11:50:38.330Z END RequestId: 3e331a19-31d9-4342-af21-fe0e7e023c6e
As you can see, the request c2cfe6d6-10d3-4520-8ef1-c82730103331
started at 11:50:32.134Z
. I expect the next request to start around 11:51:32
, but that’s starting only after 3 seconds (at 11:50:35.698Z
). I wonder why it’s working like this.
I tried removing the provisionedConcurrency
setting altogether and changing the rate(1 minute)
schedule to cron(* * * * ? *)
without success. What am I doing wrong?
Is it because the lambda is getting concurrently executed sometimes? If so, is there a way to stop that?
2
Answers
Adding
reservedConcurrency: 1
to the lambda definition onserverless.yml
resolved the issue. It made sure to have only one active instance running (all the other invocations are throttled)BUT it's still being invoked more than one time per minute 🫠
If I understood your situation correctly, your Lambda function is being invoked by an EventBridge schedule.
To the best of my knowledge, EventBridge can invoke the same target more than once: https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-troubleshooting.html#eb-rule-triggered-more-than-once
I have experience the same behaviour with Amazon SQS and S3 events. Essentially these services are distributed to guarantee at least once delivery. This means they may (and they will) deliver same message or event more than once which consequently will invoke your lambda function twice or even more.
General rule of thumb, is to ensure your code can handle duplicate messages/events. This blog explain one solution out of many possible solutions: https://aws.amazon.com/blogs/storage/manage-event-ordering-and-duplicate-events-with-amazon-s3-event-notifications/