Introduction

In this guide, we will explore the seamless integration of AWS Eventbridge and Lambda to create a robust and serverless solution for running AWS cron jobs. 

Whether it’s daily data backup, data processing, start/stop servers, or scheduled task operations, this approach ensures simplicity, efficiency, and scalability in managing tasks in your AWS environment.

What are Cron Jobs?

Cron jobs are scheduled tasks that will themselves be completed by calling some other entity to complete the given task. 

Example: We need a database backup every 15 minutes. So rather than taking a manual snapshot, which is a lengthy process, why not take help from some cool automation technique? Here, we need cron jobs.

You can go through the below link to learn more about Cron jobs.

What is Cron Job – Javatpoint

Section 1: Creating an AWS Lambda Function

  1. Log in to the console and create one lambda function named RDS_backup, or your choice. Please choose Python as a runtime language.
  2. Copy the lambda function code from the Github site and paste it into the code window. This is simple code; you just have to pass your already created RDS name, and it will take a screenshot every 15 minutes. We suggest that you please take some time to understand the code.
  3. This is all about the lambda section, configuring all the necessary things. We will mention this function as a target in a later section. 
  4. If you are stuck creating a lambda, take help from the below gif.
  5. Want to explore the serverless world? Go through this link: What is AWS Lambda? – AWS Lambda

Section 2: Creating an AWS RDS (Relational Database)

  1. Now search RDS in the search bar of the AWS console, and you will get an RDS page with lots of options.
  2. Click on Create Database and start configuring options. Choose a suitable database engine, class, and type.
  3. Give a suitable name to your database with only a small case letter. No symbol/No underscore.
  4. After all is done, click on Create Database; it will take around 10 minutes.
  5. If you need help, please follow the below gif.
  6. Explore different database offerings by AWS here: Amazon Relational Database Service (RDS) 

Section 3: Setting Up an EventBridge Rule

  1. Navigate to the Amazon EventBridge homepage and check the options available on the left side panel.
  2. Go to the scheduler option and click on schedules.
  3. Scroll down and click on the Create Schedule button. Now, you will be prompted on the configuring steps of the schedule.
  4. Please include all the necessary options like name, target, setting, etc.
  5. Creating cron expressions is the main task, we are using it every 15 min on MON-FRI; if your business needs are different, you can create them accordingly.
  6. If you are new to this, please go through the below documents:
    Creating an Amazon EventBridge rule that runs on a schedule

Section 4: Testing the setup

  1. You just need to test lambda, and that’s all. Your EventBridge rule and database will work properly.
  2. Let us clear the workflow. The time/scheduled EventBridge rule will be triggered, and then it will invoke the lambda function. After invocation, lambda does the rest, like creating a snapshot, which is a kind of backup from your database.
    Eventbridge โ€”>  Lambda โ€”-> Rds Snapshot
  3. You have already created a lambda function. Just change the value of DBinstanceIdentifier and try to run. If you get status 200, then you are good; otherwise, you need troubleshooting.
  4. As far as we know, you will probably get an authorization error because the lambda role does not have all the access to create a snapshot. To troubleshoot this, attach the necessary permission to the lambda role.

Section 5: Monitoring

  1. All your setup cron jobs are working perfectly fine at this point, and you need to check whether the rule is invoking lambda. Lambda takes screenshots periodically; let’s check all this out in this section.
  2. Now, let’s check if Lambda is invoked correctly every 15 minutes. For that, go to the lambda console, functions, and click on your function, then go to the monitoring tab and click View Logs in Cloudwatch.
  3. Then you will land on the Cloudwatch page lambda log group; scroll down, and you can check all the invocations with timestamps.

  1. Now let’s check if Lambda is taking snapshots of RDS. For that, you need to go to the RDS homepage and click on snapshots, which are present on the left-side option panel. Here, you can check all the snapshots for the exact time at which the lambda function was triggered.
  2. You can check out the below gif for more assistance.

Conclusion

In today’s world, there is no need to do recurring activities manually; automation is everywhere. With the help of this blog at CloudZenia, we are showcasing how we can automate daily repeated activities with the help of list cron jobs, which overall reduce manual intervention. The main point is that you should be able to create cron expressions for particular business needs.

We have added useful links for every section. Please review it to better understand all the core concepts, and visit CloudZenia for more.

Dec 27, 2023