How To Schedule a Lambda Function in AWS

How To Schedule a Lambda Function in AWS

Schedule a lambda function as a cron job

AWS Lambda function is a FaaS(Function as a Service) offered by AWS. FaaS is a cloud computing model that falls under the umbrella of serverless computing.

AWS Lambda function is beneficial when we want to execute our code without worrying about the underlying servers and resources. No matter how many users hit your lambda function, it works smoothly as AWS maintains the underlying infrastructure.

Now what if you want your lambda function to get executed only at a particular time, or at some scheduled time daily, hourly, weekly, or whenever you want?

Here Cloudwatch EventBridge (Earlier known as Cloudwatch Events) comes into the picture. You can schedule your aws services to perform at some particular time or if a particular event occurs. In this article, we'll focus on how to automate the lambda function to get executed at some frequency of time with the help of Cloudwatch EventBridge.

Creating a Lambda Function

First login to your AWS console and then search for lambda service

Now open the lambda service and create a lambda function (if you already have a lambda function then you can skip this part).

On the next page, click on Create a function

Now on the Create Function page, enter the name of the lambda function, and choose your programming language and architecture.

Note:- You can also use a blueprint of a pre-created function or container image for your function, but we are continuing from scratch.

Now let other settings be on default and click on Create function.

Now on the next page, write your code, deploy the function, and test it. The function should be running properly.

Now our function is ready, let's create a schedule for it.

Creating EventBridge Rule

First search for EventBridge Service and open it.

After opening the EventBridge page, select EventBridge Rule and then click on Create Rule.

After clicking on Create Rule button, enter some basic details for the event you are going to create and then choose Schedule for a scheduled event rule.

Below you can see two options Continue to create rule and Continue in EventBridge Scheduler. For this demo, we'll go with Continue to create rule.

Now on the next page, you can define your schedule as a cron expression. choose your cron expression as per your need and click on Next.

On the next page, choose your lambda function and again click on Next.

On the next page, you can add tags, that are completely optional. Now click on Next and review and create your rule.

Now your EventBridge rule has been created. You can also verify this in your lambda function, you can see that the EventBridge rule is triggering the lambda function.

Below, Inside the Monitor tab, you can verify that my lambda function is getting executed every minute (Because I set my cron expression for every minute).

So this is how you can schedule your lambda function.

Conclusion

We have seen how you can create your lambda function. and we automated it using the EventBridge rule.

Hope you enjoyed it and found it useful.


Recommended Article:

Creating custom cloudwatch metrics for ec2 servers

Did you find this article valuable?

Support Adil Ansari by becoming a sponsor. Any amount is appreciated!