0

I know it's possible to make a Lambda function run on a schedule using CloudWatch (which basically simulates CRON).

However, would it be possible to do so on a temporary basis, based on a manual trigger? For example, "click a button" to run a Lambda function every hour, but only for the next 48 hours - and then stop, and not start again until a human "presses the button".

The "button" could create a CloudWatch rule using the API, but how can I make the rule stop automatically/by itself after 48 hours?

Example of CloudWatch rule creation:

aws events put-rule \
--name my-scheduled-rule \
--schedule-expression 'rate(1 hour)'
Marco Roy
  • 4,004
  • 7
  • 34
  • 50
  • 1
    Perhaps you could write the Lambda function so that the last thing it does is to check an environment variable or Parameter Store for a decommission/die time. When it exceeds that time it would remove the CloudWatch scheduled event. Next time you want to start it up, your start process writes the new decommission/die time and then you configure CloudWatch to again schedule the Lambda. – jarmod Oct 11 '19 at 01:16
  • Oh, that would work! Mind posting it as an answer? – Marco Roy Oct 11 '19 at 01:18
  • 1
    Or, just create 48 non-repeating events! – John Rotenstein Oct 11 '19 at 01:44
  • @JohnRotenstein: That would work too! It's a bit trickier as the duration can be dynamic, but wouldn't be too hard to implement with a simple loop. – Marco Roy Oct 11 '19 at 02:09

4 Answers4

2

Another option could be to use an AWS Step Function to trigger the disabling/deletion of the initial/repeating CloudWatch event, as described in this post.

Marco Roy
  • 4,004
  • 7
  • 34
  • 50
2

Perhaps you could write the Lambda function so that the last thing it does is to check an environment variable or Parameter Store for a decommission/die time. When it exceeds that time it would remove the CloudWatch scheduled event. Next time you want to start it up, your start process writes the new decommission/die time and then you configure CloudWatch to again schedule the Lambda.

jarmod
  • 71,565
  • 16
  • 115
  • 122
1

One option would be to use create a second CloudWatch event triggered via CRON & scheduled to run once in the future (i.e. 48 hours from now), as explained in this post.

This could trigger a Lambda function which would disable or delete the initial, repeating CloudWatch schedule.

Marco Roy
  • 4,004
  • 7
  • 34
  • 50
1

Using Step functions is a clean work but there is also a smart way I have recently seen some articles about to use a feature in DynamoDB as explained here for Ad-hoc scheduled tasks that might work for your case too: https://theburningmonk.com/2019/03/dynamodb-ttl-as-an-ad-hoc-scheduling-mechanism/

Rez.Net
  • 1,354
  • 2
  • 19
  • 28