How To Schedule a Lambda Function in AWS
Tags: aws, serverless, lambda, tutorial
Published: 2023-11-11 08:01:32

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

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684573466200/cf39cbe8-c21a-4014-86d2-897621ebbdd0.png)

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*
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684573585627/dcae6a15-00b0-4401-bbf0-9600b7330caa.png)

Now on the Create Function page, enter the name of the lambda function, and choose your programming language and architecture.
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684573740019/418ee246-e255-46ab-8eb0-a2f5d4e9b7dd.png)
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684573881892/c8fce81c-2bbc-4738-9a76-f30a6d0010b5.png)

*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.
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684578091122/5d401943-9a25-475c-b145-1acfbd5822f3.png)

![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684574402324/13d7129f-6566-4202-ab5d-04616d5e85b6.png)

![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684574464002/f33a2706-c0db-4fef-830e-62528a94002e.png)

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

##Creating EventBridge Rule
First search for EventBridge Service and open it.
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684578416844/d921b36d-febc-4c9f-8e08-2e02f08e843c.png)

After opening the EventBridge page, select EventBridge Rule and then click on Create Rule.
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684578506536/5336cf6d-c981-4402-94c5-c6cbf01f7a05.png)

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*.
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684578909962/f4ab7440-35ac-4df8-959a-b0632bf1383a.png)

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*.
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684579796202/2db57ecb-146e-42ee-83c0-b7f74d61a07a.png)

On the next page, choose your lambda function and again click on *Next*.
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684579899384/86cb9d17-5bd4-4cd3-a8c3-5515480e89de.png)

On the next page, you can add tags, that are completely optional. Now click on *Next* and review and create your rule.
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684579953773/67cc4543-653e-4054-bc37-698bb19002ab.png)

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.
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684580111900/42a70d44-7ae1-47c7-8073-d3c556a04bdc.png)


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).
![Image Description](https://cdn.hashnode.com/res/hashnode/image/upload/v1684580538523/992c4418-4830-43ff-9455-110bc59ae984.png)

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.