使用 EventBridge 定时调用
学习使用 Amazon EventBridge 规则,通过 cron 和速率表达式按周期运行 Lambda 函数。
使用 EventBridge 定时调用 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Serverless AWS Lambda Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Serverless AWS Lambda Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Scheduled Functions?
Many tasks need to run on a timer: nightly reports, cache warming, cleanup jobs. EventBridge Scheduler triggers Lambda without any always-on server.
Rate vs Cron
EventBridge supports two schedule syntaxes:
- rate() for simple intervals.
- cron() for precise calendar times.
A Rate Expression
A rate expression runs every fixed interval. For example rate(5 minutes) fires every five minutes.
rate(1 hour)
rate(5 minutes)
rate(7 days)A Cron Expression
Cron gives calendar precision. AWS cron has six fields: minute, hour, day-of-month, month, day-of-week, year.
cron(0 8 * * ? *)
cron(0 0 1 * ? *)
cron(0/15 * * * ? *)The Scheduled Event Payload
The Lambda receives a JSON event describing the trigger. Scheduled events have a fixed shape with source aws.events.
{
"source": "aws.events",
"detail-type": "Scheduled Event",
"time": "2026-05-29T08:00:00Z"
}Handling a Scheduled Event
Your handler ignores most of the payload and simply does its periodic work.
def handler(event, context):
print('Running scheduled job at', event.get('time'))
# do cleanup, send report, etc.
return {'status': 'done'}Creating the Rule
You attach a schedule to a Lambda as an EventBridge rule with the Lambda as its target. The CLI shows the core idea.
aws events put-rule \
--name nightly-cleanup \
--schedule-expression 'cron(0 2 * * ? *)'Granting Permission
EventBridge must have permission to invoke your function. Add a resource policy allowing the events.amazonaws.com principal.
aws lambda add-permission \
--function-name cleanup \
--statement-id eb-invoke \
--action lambda:InvokeFunction \
--principal events.amazonaws.comTime Zones
Classic EventBridge rules run in UTC. The newer EventBridge Scheduler supports named time zones, which avoids manual offset math.
Idempotency Matters
A schedule may occasionally fire twice. Make scheduled jobs idempotent so a repeat run causes no harm.
Disabling a Schedule
You can disable a rule without deleting it, which is useful for pausing a job during maintenance windows.
aws events disable-rule --name nightly-cleanupQuick Check
Test your scheduling knowledge.
Recap
You can now schedule Lambda functions with EventBridge using rate and cron expressions, grant invoke permission, mind time zones, and keep jobs idempotent.
用 AI 导师学习 Serverless AWS Lambda Development — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「使用 EventBridge 定时调用」课时是免费的吗?
是的 — 「使用 EventBridge 定时调用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。
「使用 EventBridge 定时调用」这节课中我会学到什么?
学习使用 Amazon EventBridge 规则,通过 cron 和速率表达式按周期运行 Lambda 函数。 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Serverless AWS Lambda Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Serverless AWS Lambda Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 EventBridge 定时调用」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Serverless AWS Lambda Development 课中编写并运行代码吗?
能。每节 Serverless AWS Lambda Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 事件驱动架构基础
- 使用 API Gateway 调用 Lambda
- S3 与 SQS 事件触发器
- 使用 EventBridge 定时调用