In addition to this, AWS CloudWatch can also be used to set up
cron jobs in the cloud. This feature helps a Lambda function to be invoked periodically, say every five mins.
Wait, but how do you schedule a job to invoke the Lambda function only once ??
AWS Step Functions to the rescue. AWS Step Functions helps you orchestrate the tasks using the serverless approach. You can build your workflow which consists of states and initiate an execution. AWS Step Functions can be considered a simplified version Amazon SWF.
Following is the approach to creating a state machine using Step Functions to schedule an invocation of a Lambda function.
Create a State Machine  with two states.
First state (CountDownTimer) is a “Wait” state, which waits for the specified amount of time and calls the next state (State B)
Second state (InvokeLambda) is a “Task” state, which invokes a Lambda.
Execute the State Machine any number of times with the “Timestamp” as the input.
Following are the Amazon States Language (ASL) (schedule-lambda-asl.json) for creating a State Machine and the input (schedule-lambda-input.json) to its execution.
- Replace the Resource field value with your Lambda ARN.
- Mention the invocationTime value in UTC.
To create a state machine, execute the following the command using AWS CLI.
On successful execution, this command creates a state machine named OneTimeLambdaScheduler and returns the
stateMachineArn of it. Copy the
To schedule the invocation, you have to create a new execution of the State Machine we just created using the following command.
That’s it. Your Lambda function will be triggered once at the specified time.