ScanSkill
Sign up for daily dose of tech articles at your inbox.
Loading
AWS

Schedule Cron Jobs in AWS Lambda With Event Bridge

Schedule Cron Jobs in AWS Lambda With Event Bridge
Schedule Cron Jobs in AWS Lambda With Event Bridge

In this article, we will learn how to schedule cron jobs in AWS lambda. A cron job is a utility program used for scheduling tasks to be executed sometime in the future. The flexibility of cron jobs makes them ideal for repetitive tasks like backups and system cleaning, but also data fetching and data processing. For example, send a daily attendance report to HR at the end of the day. To schedule a cron job in an AWS environment we need AWS Lambda, Cloud Watch, and Event Bridge.

AWS  Lambda is an Amazon Web Service compute service that lets you run code without managing or provisioning servers. In Lambda, your code is executed on a high-availability compute infrastructure, and all of the compute resources are managed, including server and operating system maintenance, capacity provisioning, automatic scaling, and logging.

AWS CloudWatch is a monitoring and management service that provides actionable insights into applications and infrastructure on AWS, hybrid clouds, and on-premises. CloudWatch enables users to view and collect monitoring data for AWS infrastructures in one place. The CloudWatch platform provides data collection, monitoring, automated actions, compliance, and security features.

Amazon EventBridge is an event bus that can be used to build events-driven applications at scale using events generated from applications, SaaS applications, and AWS services. Using EventBridge, you can stream real-time data from applications like Zendesk or Shopify to AWS Lambda and other SaaS applications.

Prerequisites

  1. AWS account
  2. Basic AWS Lambda and EventBridge concepts

How to schedule cron jobs in AWS lambda with Event Bridge?

Step 1: Create a Lambda function

  • Go to AWS Console and Log In
  • In the search bar, type lambda
  • Select the Lambda options displayed under Services section
  • After that AWS Lambda page will appear. Then click the Create function button on the right side of the page.
  • Now, let’s create a function named scheduleCronJobFunction and create a new role for lambda under the Execution role section. Then click Create Function.
  • It will take some time to create a lambda function and you will be redirected to the following page.
  • Now, Let’s modify the index.js file so that AWS Cloudwatch will log the time when a cron job executes our lambda.
exports.handler = async (event) => {
    // TODO implement
    const date = new Date();
    console.log("Cron job successfully executed on:", date.toUTCString());
};

Step 2: Create cron jobs using Event Bridge

  • Now, let’s create a cron job using AWS Event Bridge so that we can execute our lambda every 5 mins.
  • In the search bar, type lambda
  • Select the Lambda options displayed under Services section
  • After that AWS EventBridge page will appear. Then click the Create rule button on the right side of the page.

Step 3: Define a schedule rule detail

  • Let’s enter a rule name as desired, we will use first-cron-job for this article and choose the Schedule option under the Rule Type as shown below.

Step 4: Define schedule pattern

  • Now, let’s define our schedule pattern. We will schedule the cron job to execute every 5 min so that our lambda will log the time at the interval of 5 mins.

Step 5: Assign target Lambda to the rule

  • After completing the schedule pattern, let’s select the lambda function which will be triggered by the cron job.
  • Configuration of the tag is optional so click next without modifying anything.
  • Finally, let’s all the change we made and click Create rule if all the setting is the same as we have selected previously.
cron jobs in aws lambda
  • Now, a new rule will be created in our Event Bridge.

Step 6: Monitor the Lambda Response

  • After 5 min of the rule creation, we can check our logs in cloud watch. For that, let’s go to our lambda detail page and choose Monitor tab.
  • Then click View logs in CloudWatch button.
  • Now, we will be redirected to the cloud watch page.
  • Under the Log streams tab, different log streams will be listed according to the event time.
  • Click a record to view the detail of the log stream.

Conclusion

In this article, we have learned how to schedule cron jobs with AWS Lambda. You can also follow this link to simply implement the cron job on node js.

Sign up for daily dose of tech articles at your inbox.
Loading