简体   繁体   English

从s3上传到一定时间范围内触发lambda函数

[英]Trigger lambda function in a certain time range from s3 upload

Currently, We are triggering our lambda function from S3 Upload.目前,我们正在从 S3 Upload 触发我们的 lambda 函数。 S3 upload is not time-specific, it can happen at any time. S3 上传不是特定时间的,它可以随时发生。

Now we got a new requirement that we can run lambda within a certain time range only eg between 13 UTC to 20 UTC.现在我们有了一个新要求,我们只能在某个时间范围内运行 lambda,例如 13 UTC 到 20 UTC 之间。 That means if the s3 upload happened at 5 UTC, lambda will be triggered at 13 UTC.这意味着如果 s3 上传发生在 5 UTC,则 lambda 将在 13 UTC 触发。

What is the best approach to implement this change?实施此更改的最佳方法是什么?

Any suggestions would be appreciated.任何建议,将不胜感激。 Thanks in advance.提前致谢。

If the condition is that the upload must be done between the time range specified, then nothing special needs to be done.如果条件是必须在指定的时间范围内完成上传,则无需进行任何特殊操作。

If you want the upload to trigger the lambda which will only be executed once the time range is met, then you could use AWS Step Functions.如果您希望上传触发仅在满足时间范围后执行的 lambda,那么您可以使用 AWS Step Functions。 Using a Wait state, a task time can delay the invocation of the lambda function.使用等待状态,任务时间可以延迟 lambda 函数的调用。 The delay can be dynamic (eg 13 UTC - current time).延迟可以是动态的(例如 13 UTC - 当前时间)。

The workflow can look like this:工作流程可能如下所示:

  1. File is uploaded to S3文件上传到 S3
  2. Notification gets sent to EventBridge通知被发送到 EventBridge
  3. EventBridge starts state machine EventBridge 启动状态机
  4. Wait state for a certain time period等待一段时间的状态
  5. Lambda gets triggered once wait state is done等待状态完成后触发 Lambda

See:看:

Since the timing to trigger your Lambda function is now fixed, you can setup CloudWatch Trigger at the desired time and set the Lambda function as the source for that event rule.由于触发 Lambda 函数的时间现已固定,您可以在所需时间设置 CloudWatch 触发器并将 Lambda 函数设置为该事件规则的源。

Steps -脚步 -

  1. Open the Functions page of the Lambda console.打开 Lambda 控制台的函数页面。

  2. Choose a function选择一个功能

  3. Under Function overview, choose Add trigger.在函数概览下,选择添加触发器。

  4. Set the trigger type to EventBridge (CloudWatch Events).将触发器类型设置为 EventBridge (CloudWatch Events)。

  5. For Rule, choose Create a new rule.对于规则,选择创建新规则。

  6. Configure the remaining options and choose Add.配置其余选项并选择添加。

Rule is nothing but a cron pattern which you need to set for the desired time.规则只不过是您需要为所需时间设置的 cron 模式。 Once the rule is implemented, the trigger will start according to the cron, and run the lambda function.规则执行后,触发器将根据 cron 启动,并运行 lambda 函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM