AWS Lambda安排延迟执行一次运行 [英] AWS Lambda schedule a delayed execution to run once
问题描述
我有一个背后带有Lambda的API网关,对于某些我想在将来安排执行的端点,可以运行一次,例如REST调用是在T时间进行的,我希望该Lambda安排执行我在T + 20分钟时一次。
I have an API Gateway with Lambdas behind, for some of the endpoints I want to schedule an execution in the future, to run once, for example the REST call was made at T time, I want that lambda to schedule an execution ONCE at T+20min.
我发现实现此目标的唯一解决方案是在进行REST调用时使用boto3和Cloudwatch设置cron,然后发送
The only solution I found to achieve this is to use boto3 and Cloudwatch to setup a cron at the moment the REST call was made, send an event with the payload, then when the delayed lambda runs, it removes the rule.
我发现这很重,还有其他方法可以实现这种模式吗?
I found this very heavy, is there any other way to achieve such pattern ?
p>
编辑:这不是一个Lambda,只是运行一次。
It is NOT A RECURRING Lambda, just to run ONCE.
推荐答案
一种选择是在给定的延迟后使用 AWS Step Functions 触发AWS Lambda函数。
One option is to use AWS Step Functions to trigger the AWS Lambda function after a given delay.
Step Functions具有等待状态,可以计划或延迟执行,因此您可以实现一个相当简单的Step Functions状态机,该状态机在调用Lambda函数之前要延迟连接。不需要数据库!
Step Functions has a Wait state that can schedule or delay execution, so you can can implement a fairly simple Step Functions state machine that puts a delay in front of calling a Lambda function. No database required!
有关概念的示例(略有不同,但足够接近),请参见:
For an example of the concept (slightly different, but close enough), see:
- Using AWS Step Functions To Schedule Or Delay SNS Message Publication - Alestic.com
- Task Timer - AWS Step Functions
这篇关于AWS Lambda安排延迟执行一次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!