AWS Lambda 计划任务 [英] AWS Lambda Scheduled Tasks
问题描述
亚马逊发布了 AWS Lambda(
将预定事件添加到现有的 lambda
导航到 lambda 的触发器"选项卡,选择添加触发器",然后指定CloudWatch 事件 - 计划"触发器.我有一个带有 SNS 触发器的现有 lambda 的示例屏幕截图:
加载后,用于配置此触发器的 UI 与上面将计划事件添加到新 lambda"部分中的屏幕截图相同.
讨论
对于您的示例,您需要使用 cron()
而不是 rate()
.lambda 中的 Cron 表达式需要所有字段,并以 UTC 表示.因此,要每天在下午 5 点 (UTC) 运行一个函数,请使用以下 cron 表达式:
cron(0 17 * * ? *)
更多资源
- AWS 文档 - 使用调度表达式评分或 Cron
- AWS 文档 - 使用 AWS 按计划运行 AWS Lambda 函数命令行
- AWS 文档 - 教程:将 AWS Lambda 与 Scheduled 结合使用活动
- AWS 提供了一个示例蓝图",它使用名为
lambda-canary
的 cron 表达式,可以在从 AWS 控制台创建函数期间选择该表达式. - 本教程将引导您完成此蓝图的配置.
- AWS 提供了一个示例蓝图",它使用名为
注意事项
- 自此功能首次发布以来,此事件类型的名称已从计划事件"更改为CloudWatch 事件 - 计划".
- 在此功能发布之前,此问题的推荐解决方案(根据 "AWS Lambda 入门" 在 42 分 50 秒) 是使用 SWF创建计时器,或使用外部应用程序创建计时器.
- 自从预定的活动博客文章发布以来,Lambda 用户界面已经过大修,其中的屏幕截图不再准确.有关最新修订,请参阅我在 2017 年 3 月 10 日更新的屏幕截图.
Amazon announced AWS Lambda (http://aws.amazon.com/lambda/).
The product description includes:
Scheduled Tasks
AWS Lambda functions can be triggered by external event timers, so functions can be run during regularly scheduled maintenance times or non-peak hours. For example, you can trigger an AWS Lambda function to perform nightly archive cleanups during non-busy hours.
When I read this, I understood I could finally have a way to consistently do "cron-like" tasks. I want to run a specific query everyday at 5PM let's say.
However I do not find this anywhere in the documentation. They only mention triggers on programatical events, or events from other AWS services.
Did I misunderstand? Or can someone point me to the documentation?
Native Support for Scheduled Events added October 8, 2015:
As announced in this AWS blog post, scheduling is now supported as an event source type (also called triggers) called "CloudWatch Events - Schedule", and can be expressed as a rate or a cron expression.
Add Scheduled Event to a new lambda
Navigate to the 'Configure triggers' step of creation, and specify the 'CloudWatch Event - Schedule' trigger. Example configuration below:
Add Scheduled Event to an existing lambda
Navigate to the 'Triggers' tab of your lambda, select 'Add Trigger', and specify the 'CloudWatch Event - Schedule' trigger. Example screenshot where I have an existing lambda with an SNS trigger:
Once loaded, the UI to configure this trigger is identical to the screenshot in the above "Add Scheduled Event to a new lambda" section above.
Discussion
For your example case, you'll want to use cron()
instead of rate()
. Cron expressions in lambda require all fields and are expressed in UTC. So to run a function every day at 5pm (UTC), use the following cron expression:
cron(0 17 * * ? *)
Further Resources
- AWS Documentation - Schedule Expressions Using Rate or Cron
- AWS Documentation - Run an AWS Lambda Function on a Schedule Using the AWS CLI
- AWS Documentation - Tutorial: Using AWS Lambda with Scheduled Events
- AWS has provided a sample "blueprint" that uses a cron expression called
lambda-canary
that can be selected during function creation from the AWS console. - This tutorial walks you through configuration of this blueprint.
- AWS has provided a sample "blueprint" that uses a cron expression called
Notes
- The name of this event type has changed from "Scheduled Event" to "CloudWatch Events - Schedule" since this feature was first released.
- Prior to the release of this feature, the recommended solution to this issue (per "Getting Started with AWS Lambda" at 42min 50secs) was to use SWF to create a timer, or to create a timer with an external application.
- The Lambda UI has been overhauled since the scheduled event blog post came out, and the screenshots within are no longer exact. See my updated screenshots above from 3/10/2017 for latest revisions.
这篇关于AWS Lambda 计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!