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:
- It is Fully managed, works on pay as you go model.
- Native integration with SaaS providers.
- Can integrate with 90+ AWS services as a source.
- Can integrate with 17 AWS services as a target.
- It charges $1 per million for events put into the bus.
- 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.
Click on Create 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.
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:
Save this function by clicking on Deploy.
Step3: Finally we will schedule it using AWS EventBridge. Let’s move to 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.
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.
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.
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.
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.
Click on this to see the log events.
You can find here the lambda function triggering after every minute. It’s printing the event details also.
So, we have successfully scheduled the lambda function using AWS EventBridge.
Congrats! 👏 We are done now!
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.