How to Trigger AWS Lambda on a Schedule Using EventBridge
You can trigger a
AWS Lambda function on a schedule by creating an EventBridge rule with a cron or rate expression and setting the Lambda function as the target. This lets your Lambda run automatically at fixed times or intervals without manual intervention.Syntax
To trigger a Lambda on schedule, you create an EventBridge rule with a schedule expression and add your Lambda function as the target. The key parts are:
- ScheduleExpression: Defines when the Lambda runs, using
rate()orcron()syntax. - Targets: The Lambda function ARN that will be invoked.
- State: Enables or disables the rule.
bash
aws events put-rule --name MyScheduledRule --schedule-expression "rate(5 minutes)" --state ENABLED aws events put-targets --rule MyScheduledRule --targets Id=1,Arn=arn:aws:lambda:region:account-id:function:MyFunction
Example
This example creates an EventBridge rule that triggers a Lambda function every day at 6:00 AM UTC using a cron expression.
bash
aws events put-rule --name DailyMorningTrigger --schedule-expression "cron(0 6 * * ? *)" --state ENABLED aws events put-targets --rule DailyMorningTrigger --targets Id=1,Arn=arn:aws:lambda:us-east-1:123456789012:function:MyDailyFunction aws lambda add-permission --function-name MyDailyFunction --statement-id DailyTriggerPermission --action 'lambda:InvokeFunction' --principal events.amazonaws.com --source-arn arn:aws:events:us-east-1:123456789012:rule/DailyMorningTrigger
Output
Rule created: DailyMorningTrigger
Targets added to rule: DailyMorningTrigger
Permission added to Lambda function: MyDailyFunction
Common Pitfalls
- Missing Lambda permission: You must add permission for EventBridge to invoke your Lambda, or the trigger will fail.
- Incorrect schedule expression: Using wrong cron syntax or rate format causes errors or unexpected schedules.
- Wrong region or ARN: The Lambda ARN and EventBridge rule must be in the same AWS region.
- Disabled rule: If the rule state is not ENABLED, the Lambda won't trigger.
bash
Wrong (missing permission): aws events put-rule --name TestRule --schedule-expression "rate(1 hour)" --state ENABLED aws events put-targets --rule TestRule --targets Id=1,Arn=arn:aws:lambda:us-east-1:123456789012:function:MyFunction Right (add permission): aws lambda add-permission --function-name MyFunction --statement-id TestRulePermission --action 'lambda:InvokeFunction' --principal events.amazonaws.com --source-arn arn:aws:events:us-east-1:123456789012:rule/TestRule
Quick Reference
Use these tips to set up scheduled Lambda triggers smoothly:
- Use
rate(value unit)for simple intervals (e.g., every 5 minutes). - Use
cron(fields)for complex schedules (e.g., specific time of day). - Always add Lambda permission for EventBridge to invoke your function.
- Keep Lambda and EventBridge rule in the same AWS region.
- Enable the rule to activate the schedule.
Key Takeaways
Create an EventBridge rule with a schedule expression to trigger Lambda on a schedule.
Add your Lambda function as a target to the EventBridge rule.
Grant EventBridge permission to invoke your Lambda function.
Use correct cron or rate syntax for the schedule expression.
Ensure the rule is enabled and in the same region as your Lambda.