Skip to content

Lambda Function Triggering Twice

0

Hello, I am new to AWS. I built out a small lambda function that sends out an email once a month. The lambda function has an EventBridge trigger that is set up with a CRON expression of 0 14 1 * ? *. This should have the function run every 1st of the month at 14:00 UTC time. Enter image description here

However, the behavior that I am seeing is that the lambda function is trigger twice on the first of the month. Once at 13:00 UTC and again at 14:00 UTC. Enter image description here

Am I misunderstanding something. How is it possible? It seems like it should be straightforward that the expression will run at 14:00 UTC, yet I am getting two runs that are an hour apart.

Looking at the EventBridge itself, there is only one run being shown which is the 14:00 UTC run, which is correct, but something seems to be triggering the lambda function at 13:00. In the lambda function, under Configurations => Triggers, only the one trigger is showing.

asked a year ago628 views

2 Answers
0

The issue you're experiencing with your Lambda function being triggered twice is not uncommon and can happen for several reasons, even when your EventBridge rule configuration appears correct.

Based on your description, you have a single EventBridge rule with a CRON expression of 0 14 1 * ? * which should trigger your Lambda function only once at 14:00 UTC on the first day of each month. However, you're observing an additional execution at 13:00 UTC.

Here are some possible explanations for this behavior:

  1. Multiple triggers: Even though you only see one trigger in the Lambda configuration, there might be another service or event source invoking your function. Lambda functions can have multiple triggers, each acting independently.

  2. Lambda's execution behavior: AWS Lambda is designed with high availability in mind, which sometimes means it cannot guarantee exactly-once execution. In some cases, Lambda might execute a function twice due to internal service operations like load balancing or scaling activities.

  3. Retry mechanisms: If your function encountered an error or timeout during execution, Lambda might automatically retry the invocation, leading to what appears to be multiple executions.

  4. Time zone considerations: If you're looking at logs in a different time zone, it might appear as if the function is running at different times when it's actually just one execution.

To troubleshoot this issue:

  1. Check CloudWatch Logs for both executions to see if they have identical event data or if there are any error messages.

  2. Implement idempotency in your Lambda function to ensure that even if it runs multiple times, it won't send duplicate emails.

  3. Add logging at the beginning of your function to capture and compare the event data from both executions.

  4. Check if there are any other AWS services in your account that might be invoking this Lambda function.

  5. Review the Lambda metrics in CloudWatch to see if there are any errors or throttling events that might be causing retries.

If this continues to be an issue, you might need to implement a deduplication mechanism using a unique identifier for each scheduled event to prevent sending duplicate emails.
Sources
Invoking Lambda with events from other AWS services - AWS Lambda
lambda function is executed twice without an additional trigger | AWS re:Post
Lambda Invoke Manually from jenkins using AWS Lambda Invoke | AWS re:Post

answered a year ago

EXPERT

reviewed a year ago

0

Hello.

EventBridge may run multiple times in rare cases.
However, it only runs multiple times on rare occasions, so if it's running multiple times each month it's a bit puzzling.
As stated in the automated response to re:Post Agent, it is possible that it is being executed from somewhere other than EventBridge.
https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-troubleshooting.html#eb-rule-triggered-more-than-once

If it's running monthly, why not enable Lambda data events in CloudTrail and check the Lambda execution history?
This will allow you to identify which service is executing Lambda at 13:00.
https://docs.aws.amazon.com/lambda/latest/dg/logging-using-cloudtrail.html#cloudtrail-data-events
https://repost.aws/knowledge-center/lambda-user-invoked-function

EXPERT

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.