Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda function was trigger twice by CloudWatch event

I deployed a service written in Python2.7 using AWS Lambda, and it's about extracting data from some pages and sending results to a web app. The service is triggered by the AWS CloudWatch event (fixed rate of 5 mins).

However, I found out sometimes the service was triggered twice at a time. I got this because there were two log stream printed the same data and result but with different RequestID's. And the database had duplicate data, which showed that both worked successfully. It looked like the service was triggered twice almost at the same time for no reasons.

Does anyone experience the same thing, and how do you fix it? Or, is there a way to limit only one function can be executed at a time.

like image 845
zeck Avatar asked Sep 30 '16 12:09

zeck


People also ask

Can Lambda be triggered twice?

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

Is CloudWatch events supported event source for Lambda?

Each AWS account can have up to 100 unique event sources of the EventBridge (CloudWatch Events)- Schedule source type. Each of these can be the event source for up to five Lambda functions. That is, you can have up to 500 Lambda functions that can be executing on a schedule in your AWS account.

Can CloudWatch logs trigger Lambda?

You can use a Lambda function to monitor and analyze logs from an Amazon CloudWatch Logs log stream. Create subscriptions for one or more log streams to invoke a function when logs are created or match an optional pattern. Use the function to send a notification or persist the log to a database or storage.


1 Answers

Yes. Some AWS services have SLA of at least once delivery. I have experienced this with CloudWatch and CloudTrail. I do not know if you can limit it only once. You have to check if the data has been processed already. I overcame this by making boto3 calls in my python code before processing the data. Without knowing your situation, it is difficult to suggest a solution.

like image 169
helloV Avatar answered Sep 28 '22 05:09

helloV