Schedule AWS Lambda Function

Introduction

This article will use the AWS EventBridge service and create a rule to schedule functions. A rule watches for specific types of events. When a matching event occurs, the event is routed to the targets associated with the rule. A rule can be associated with one or more targets.

You can create rules that run on a schedule using cron or rate expressions. The rule can respond to an event or at certain time intervals. For example, to periodically run an AWS Lambda function, you can create a rule to run on a schedule. All scheduled events use UTC+0 time zone, and the minimum precision for a schedule is one minute. Your scheduled rule runs within that minute but not on the precise 0th second.

Let's Start,

Step 1. Amazon EvetnBridge

Search for the 'Amazon EventBridge' service and click on the same.

Event

Click on 'Rules' under Buses on left-hand navigations.

AWS

Click on 'Create rule'

Rules

Now we will configure the rule and set our Lambda function (MyLambdaFunction) as targeted.

Step 2. Create Rule

Provide the Rule name, Description & select Schedule and click on Continue to create rules.

Rules

Step 3. Define Schedule (Schedule Pattern)

Now, Two ways to specify the schedule patterns are cron and rate. Cron expressions offer more fine-grained schedule control, while rate expressions are simpler to express but lack fine-grained control.

EventBridge supports cron expressions and rate expressions. Rate expressions are simpler to define, and Cron expressions offer detailed schedule control. For example, with a cron expression, you can define a rule that runs at a specified time on a certain day of each week or month. In contrast, rate expressions run a rule regularly, such as once every hour or once every day.

Rules

We will use the Cron expressions (Cron expressions have six required fields separated by white space) 

cron(Minutes Hours Day-of-month Month Day-of-week Year)

Let set use the below cron expression to run the scheduler at 01:00 AM every day. Click here To learn more about schedule expressions

"cron(0 1 ? * * *)"

Rules

Note. In the above picture, we can view the next 10 trigger dates for our cron expression.

Now, we have set the scheduler. In the next step, let's configure the target to invoke our Lambda function.

Step 4. Select Target

Select the Lambda function and select your function. In this example, I have selected MyLambdaFunction.

Rules

Now, I will keep the other settings as it is and will click on 'Skip to Review and Create'.

Step 5. On the Review and Create page, click on 'Create rule'. With this step, we have successfully created a rule and scheduled our Lambda Function.

Step 6. Verify the rule 

Go to the Lambda Function; you will find that an EventBridge trigger has already been added to the function. 

MyLambdaFunction

Conclusion

In this article, we must learn how to schedule the AWS Lambha Function step by step. I think this article helps you a lot.


Similar Articles