How to Use AWS EventBridge for Scheduling Tasks
Use
AWS EventBridge to schedule tasks by creating a rule with a cron or rate expression that triggers targets like Lambda functions or Step Functions at set times. This lets you automate workflows without managing servers or cron jobs.Syntax
An EventBridge scheduling rule uses a ScheduleExpression with either a rate() or cron() expression. The rule triggers targets such as Lambda functions, ECS tasks, or Step Functions.
- rate(value unit): Runs at a fixed interval, e.g.,
rate(5 minutes). - cron(fields): Runs at specific times, e.g.,
cron(0 12 * * ? *)for noon UTC daily. - Targets: The AWS resource to invoke when the rule triggers.
bash
aws events put-rule --name MyScheduleRule --schedule-expression "rate(5 minutes)" aws events put-targets --rule MyScheduleRule --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 8 AM UTC using a cron expression.
python
import boto3 client = boto3.client('events') lambda_arn = 'arn:aws:lambda:us-east-1:123456789012:function:MyFunction' # Create the rule with a cron schedule response_rule = client.put_rule( Name='Daily8AMRule', ScheduleExpression='cron(0 8 * * ? *)', State='ENABLED', Description='Triggers Lambda daily at 8 AM UTC' ) # Add Lambda function as target response_target = client.put_targets( Rule='Daily8AMRule', Targets=[ { 'Id': '1', 'Arn': lambda_arn } ] ) print('Rule ARN:', response_rule['RuleArn'])
Output
Rule ARN: arn:aws:events:us-east-1:123456789012:rule/Daily8AMRule
Common Pitfalls
- Using incorrect cron syntax causes the rule to fail silently; always validate your cron expression.
- Not granting EventBridge permission to invoke the target Lambda results in invocation errors.
- For rate expressions, avoid using intervals less than 1 minute as they are not supported.
- For Lambda targets, you must add a resource-based policy allowing EventBridge to invoke the function.
bash
## Wrong: Missing Lambda permission # This will cause invocation failure aws events put-targets --rule MyRule --targets Id=1,Arn=arn:aws:lambda:region:account-id:function:MyFunction ## Right: Add permission for EventBridge to invoke Lambda aws lambda add-permission --function-name MyFunction --statement-id EventBridgeInvoke --action 'lambda:InvokeFunction' --principal events.amazonaws.com --source-arn arn:aws:events:region:account-id:rule/MyRule
Quick Reference
| Concept | Description | Example |
|---|---|---|
| ScheduleExpression | Defines when the rule triggers | cron(0 12 * * ? *) or rate(10 minutes) |
| Rule Name | Unique name for the EventBridge rule | MyScheduleRule |
| Target | AWS resource triggered by the rule | Lambda function ARN |
| Permissions | Allow EventBridge to invoke targets | Add Lambda invoke permission |
| State | Enable or disable the rule | ENABLED or DISABLED |
Key Takeaways
Create EventBridge rules with cron or rate expressions to schedule tasks easily.
Always grant EventBridge permission to invoke your target resources like Lambda.
Validate cron syntax to avoid silent failures in scheduling.
Use rate expressions for simple intervals and cron for complex schedules.
EventBridge scheduling removes the need to manage servers or cron jobs manually.