Search code examples
amazon-web-servicescronamazon-cloudwatch

Need Cloudwatch rule (scheduled one) that runs only once


I've understood AWS Cloudwatch Scheduled Rules (Amazon EventBridge Rules) which works by defining CRON expression.

The documentation mentions rules that triggers repetitively on a given time. Such as, run a Cloudwatch rule every 15 mins and so on.

What I want is a Rule that runs only ONCE. Any ways to achieve it? For example, run a rule after 1 hour from now, and thereafter it should never run.

Any solution?

PS: I've checked event based rule also but I don't have any event that can trigger rule.


Solution

  • You can use a CRON expression in CloudWatch Events (or EventBridge) that only specifies one point in time by simply not using wildcards/repeats in the CRON expression.

    For example, run a rule after 1 hour from now, and thereafter it should never run.

    While you cannot directly express "one hour from now on" in a CRON expression, you can build a CRON expression that will specify that absolute point in time, such as:

    cron(0 15 2 3 ? 2021)
    

    if today were the second of March 2021, at 14:00 o'clock. So, this is the CRON expression you'd use to say "one hour from now".