How to Schedule a Lambda Function using Amazon EventBridge

lambda
Reading Time: 4 minutes

Hello Readers! In this blog we will see how to schedule a lambda function using Amazon EventBridge. Before this we will get familiar with AWS EventBridge. We will see how it works and after that we will see its demo. So, let’s start.

What is AWS EventBridge?

Amazon EventBridge is a serveless eventbus service. An event is something that happened in your application (for example, an item was put into a cart). Event source and Event target is decided when you add a new feature in your application. Event source emits the events and event target receives the events.

In AWS, events are done through Amazon EventBridge rules. We create rules here. By creating rules, you can have AWS services act automatically, when changes occur in other AWS services, or in your own application.

EventBus: An Eventbus receives the event. When you create a rule, you associate it with a specific event bus, and the rule is matched only to events received by that eventbus. Your account has one default eventbus, which receives events from AWS services.

Some of its features includes:

  1. It is Fully managed, works on pay as you go model.
  2. Native integration with SaaS providers.
  3. Can integrate with 90+ AWS services as a source.
  4. Can integrate with 17 AWS services as a target.
  5. It charges $1 per million for events put into the bus.
  6. No additional cost for delivery.

Scheduling a Lambda Function using Amazon EventBridge:

Step1: Firstly Create a Lambda function.For this, Move to your AWS management console. Click on Lambda service. Interfaces like this will open.

 Amazon lambda function

Click on Create Function.

lambda function

Give here the function name. Select runtime as node.js. Leave other inputs as default. Click on Create Function.

My function is ready now.

lambda function

Step2: Now, here I want to change the index.js file. When my lambda function get triggered I want these two lines to be printed in the logs. So, add these two lines in your file if you want this to be there:

console.log("ScheduledLambda got triggered");
console.log("Event Details:", JSON.stringify(event));

After adding my index.js file looks like:

index.js

Save this function by clicking on Deploy.

Step3: Finally we will schedule it using AWS EventBridge. Let’s move to Amazon Eventbridge. 

 Amazon EventBridge

We will create a rule here. So, Click on Create rule. Give it here the name and description for your rule as you wish. For the rule type select schedule. Click on Next.

rule detail

So, I will select here the schedule pattern. I want this lambda function to be trigger every minute. You can specify here as per your need. You can also give here the cronjob. Click on next.

schedule

Now, I will select the target here. I will choose the lambda function and will select the name which I want to run. And Click on Next.

target

Configure tags is optional for you. A tag is a label that you assign to an AWS resource. I will not add any tag here if you want then you can.So, I will Click on Next.

Click on Create Rule. You can see my rule.

rules

Step4: Let’s see if it is working or not. For this move to the CloudWatch. Inside this move to Logs > Log groups. Here you will find the log groups.

log groups

Click on this to see the log events.

logs

You can find here the lambda function triggering after every minute. It’s printing the event details also. 

 Amazon EventBridge

So, we have successfully scheduled the lambda function using AWS EventBridge.

Congrats! 👏 We are done now!

Conclusion

Thank you for sticking to the end. In this blog we have seen how easily we can schedule a lambda function using Amazon EventBridge. We also got familiar with Amazon EventBridge. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions. So, I can improve my future posts to suit your needs.

HAPPY LEARNING!

Written by 

Naincy Kumari is a DevOps Consultant at Knoldus Inc. She is always ready to learn new technologies and tools. She loves painting and dancing.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading