15

I am aware of the CloudWatch recurring events that can be used to run Lambda recurringly.. but is there a way that I can trigger it on a certain time and not repeat?

Faizuddin Mohammed
  • 4,118
  • 5
  • 27
  • 51

5 Answers5

16

Using cloud watch rules - a single non-repetitive date can be assigned.

Example cron : 0 10 16 6 ? 2019 min hours day month dayOfWeek year

This will execute once on Sun, 16 Jun 2019 10:00:00 GMT as an example.

enter image description here

Reference: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html

BrettKennard
  • 171
  • 1
  • 4
12

you can provide to it a cron expression or rate. what you are looking for is the cron expression option that will let you to say when exactly to run. more info - https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html

if you want to run it once, manually, you can always trigger it with the "test" button at the top of the page. you can really be creative here, for example, you can even trigger it with an http request that will allow you to integrate it to whatever you want, super easily (Invoke a AWS Lambda function by a http request)

Ben Yitzhaki
  • 1,376
  • 16
  • 31
  • 1
    Although, it's not possible to *Not Repeat* a cron, since it can be made to repeat once a year, I'm using that and managing the deletion of the rule. – Faizuddin Mohammed Jan 31 '18 at 09:41
6

You can also use AWS Step Functions for that. It is especially useful when you want to programmatically control the execution time of events. There is also no need for polling because you schedule the function to execute at a specific point in time.

The following graphic shows how it works:

enter image description here

  1. The step function workflow is triggered by some event (e.g. http request) and supplied with a due date.
  2. The workflow starts and goes immediately in the 'WaitTillScheduled' State which only continues when the due date is over.
  3. The next state is an 'ActionEvent' which triggers the Lambda function that you want to execute. Finished.

For those that are using the serverless framework an example for a step function definition looks like this:

stepFunctions:
  validate: true
  stateMachines:
    scheduler:
      name: exampleScheduler
      definition:
        Comment: "Description of Step Function"
        StartAt: WaitTillScheduled
        States:
          WaitTillScheduled:
            Type: Wait
            TimestampPath: "$.dueDate"
            Next: ActionEvent
          ActionEvent:
            Type: Task
            Resource:
              Fn::GetAtt: [schedulerExecution, Arn]
            End: true

The dueDate is passed to the step function and the schedulerExecution is the Lambda function that executes at a certain point in time.

Big credit to Ryan Dsilva. He wrote an article about this which explains everything in detail: https://levelup.gitconnected.com/trigger-events-at-a-specific-timestamp-2527f9336128

beckersense
  • 310
  • 2
  • 12
  • 2
    Don't know why this isn't voted higher - this is by and far the best way to do it as the EventBridge scheduler requires a lot of API manipulation. Plus with the "Express" option now this makes scheduling events within 5 minutes really nice, fast and cheap. – Moe Jan 04 '23 at 04:30
  • 2
    Additionally the precision is down to the second! Which is another bonus. – Moe Jan 04 '23 at 04:34
3

You will need to make use of cloudwatch event rules. Lambda can be triggered from the rule based on the cron expression you provide. This AWS tutorial will walk you through the setup.

abiydv
  • 571
  • 2
  • 8
1

I think you are looking for a one-time schedule. For that AWS Recently(10-Nov-2022) launched a new service called EventBridge Scheduler. I have already added an answer in the following answer, please have a look.

I copied the following images from my original answer - 1 and answer - 2 to elaborate to answer your question in more detail.

enter image description here

In the target section, you can select 35 AWS Lambda function options.

enter image description here

Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58