Programmatically schedule an AWS Lambda for one time execution

Question:

I have two AWS-Lamdbda functions and I want Lambda A to determine a certain point in time like the 4. May 2022 10:00. Then I want Lambda B to be scheduled to run at this specific point in time.

I’m probably able to achieve this by programmatically creating a AWS eventbrigde rule with Lambda A and use the cron pattern to match my point in time. Inside of Lambda B I would then need to delete that rule, because it’s for a one time use.

Can one of you think of a more elegant way to achieve this ?

Thanks you for your wisdom !

Edit:

My point in time is dynamic. I call a public API to find the point for Lambda B to run, so I can’t use EventBridge directly. I plan on running Lambda A once a day to see if a new Lambda B run is necessary.

Asked By: Ludi

||

Answers:

[Edit Nov 2022]: EventBridge Scheduler

The new EventBridge Scheduler supports one-time schedules for events. The event will be invoked on the date and time you pass as the schedule expression: at(yyyy-mm-ddThh:mm:ss) in the boto3 EventScheduler client’s create_schedule API.


Here are a few more options to schedule a Lambda run at an arbitrary point in time:

Step Function

A three-state Step Function orchestrates the two Lambdas. Lambda A obtains and outputs a timestamp. A Wait State waits until the timestamp passes. Then Lambda B runs.

This approach is precise. N.B. Standard Workflow executions have a duration up to one year, so they can accomodate long waits.

DynamoDB Streams + TTL

Create a DynamoDB table with a TTL field and Dynamo DB Streams enabled. Set Lambda B as the Streams processor. Lambda A writes a record to the table with the timestamp as TTL. Shortly after the TTL timestamp passes, DynamoDB will trigger Lambda B.

This approach won’t give you the precision of the first approach, but will be cheaper if you have loads of events.

Answered By: fedonev