Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda schedule a delayed execution to run once

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.

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 ?

Edit: It is NOT A RECURRING Lambda, just to run ONCE.

like image 838
e-nouri Avatar asked Jul 08 '19 14:07

e-nouri


People also ask

How do you add a delay in Lambda?

Create a Lambda that consumes an SQS message and returns an error. Add the waiting queue as a trigger to this lambda. Execution Queue: Add the execution queue as a trigger to your Lambda (that you want to schedule). Then, to schedule your lambda you need to simply post a message to the waiting queue.

Can you trigger an AWS Lambda on a dynamic timer?

This can be accomplished by setting a CloudWatch event rule to trigger your Lambda function. On each invocation of your Lambda function, the function will need to determine its next run time and modify the event rule appropriately.

How do you trigger Lambda function every 24 hours?

To trigger a Lambda function once every 20 or 24 hours, we can schedule a trigger in CloudWatch Events. CloudWatch Events allows targets to be triggered using a Schedule Expression. A Schedule Expression can define a rate; for example, every 24 hours. Or can accept a standard cron job expression.


1 Answers

One option is to use AWS Step Functions to trigger the AWS Lambda function after a given delay.

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
like image 99
John Rotenstein Avatar answered Oct 16 '22 23:10

John Rotenstein