DEV Community

Cover image for How To Schedule a Lambda Function in AWS
Adil Ansari for AWS Community Builders

Posted on • Originally published at devopsisfun.hashnode.dev

How To Schedule a Lambda Function in AWS

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

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

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

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

Image Description

Image Description

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

Creating EventBridge Rule

First search for EventBridge Service and open it.
Image Description

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

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

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

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

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

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

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

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.

Top comments (4)

Collapse
 
mauricebrg profile image
Maurice Borgmeier

Nice article! Personally, I like to use crontab-generator.org to create the cron expressions and then paste them into the console/IaC tool. Building these by hand is super annoying...

Collapse
 
adilansari profile image
Adil Ansari

crontab-generator is a great tool to get cron expression for our required schedule.

Collapse
 
rdarrylr_21 profile image
Darryl Ruggles

Great article - thanks! Eventbridge is really cool.

Collapse
 
adilansari profile image
Adil Ansari

Yep, it is