AWS Lambda Cron Jobs: 3 Easy Steps

on Automation, AWS, AWS Lambda, Cron, Job Scheduling • April 12th, 2022

AWS LAMBDA CRON - FEATURED IMAGE

The automation of performing repetitive tasks is a very important aspect of many software companies. The transition of these companies from on-premise systems onto the cloud platforms has resulted in complications to these automating processes. Initially, Amazon used its EC2 platform combined with other third-party applications which proved to be very expensive as it required more resources to maintain coupling between the cron jobs.

AWS lambda cron jobs is a more recent and advanced solution to tackle and improve the automation of repetitive jobs on the amazons cloud platform. AWS Lambda is a cloud-based serverless cloud computing platform, that provides resources for 24/7 jobs running which is an important aspect of running AWS Lambda cron jobs. 

This article will give a comprehensive guide on aspects required to run the cron jobs and steps to schedule AWS Lambda Cron jobs.

Table of Contents

What are Cron Jobs?

AWS Lambda Cron: cron logo
Image Source: stevenmortimer.com

Cron is a utility that enables the users to schedule tasks that are repetitive at a specific time frame. It allows the users to determine the specific processes to be automated and the time frame of execution. Cron Jobs are demon processes i.e they work in the background. 

Cron jobs can automate system maintenance, scheduled backups, security checks, and many more processes. These are used mainly by system administrators to streamline the process of automation. These Linux cron jobs when run on Amazon’s Lambda server become the AWS Lambda cron jobs.

Replicate Data in Minutes Using Hevo’s No-Code Data Pipeline

Hevo Data, a Fully-managed Data Pipeline platform, can help you automate, simplify & enrich your data replication process in a few clicks. With Hevo’s wide variety of connectors and blazing-fast Data Pipelines, you can extract & load data from 100+ Data Sources Amazon S3AWS Elasticsearch, and many more straight into your Data Warehouse or any Databases.

To further streamline and prepare your data for analysis, you can process and enrich raw granular data using Hevo’s robust & built-in Transformation Layer without writing a single line of code!

GET STARTED WITH HEVO FOR FREE[/hevoButton]

Hevo is the fastest, easiest, and most reliable data replication platform that will save your engineering bandwidth and time multifold. Try our 14-day full access free trial today to experience an entirely automated hassle-free Data Replication!

What is Amazon Lambda?

AWS Lambda Cron: AWS lambda logo
Image Source: incentius.com

AWS Lambda is a computing service that is part of Amazon’s Web Services platform. It is a Serverless solution that is completely event-driven, i.e the services are run, based on the number of events the resources are decided. Lambda also provides functions that are self-contained applications, for performing operations on web pages, and using API for connecting with other AWS services.

AWS Lambda enables you to reduce efforts on the operations like handling Operating Systems(OS), access control systems, patching, provisioning, scaling, and concentrating efforts on core business and product planning and strategy.

AWS Lambda is a fully managed service, which improves the efficiency of cloud-based operations by reducing the efforts and expenses associated with buying, maintaining, and replacing the hardware. It also gives them peace of mind by removing the process of hiring the workforce required to maintain those servers as well.

Benefits of AWS Lambda

The advantages of using AWS Lambda are:

1) Reduced Expenses

AWS Lambda requires to be paid only for the resources used by your functions and any network traffic. It employs a pay-as-you-go model. The prices don’t include the unused minutes of the server as the cost is calculated solely on the time the application used the server.

2) Variety of Use Cases

The main benefit of AWS cloud is the ability to cater to a wide range of use cases like working on daily tasks, notifications, S3 objects, automated chatbots, and many more applications.

3) Fully Managed Infrastructure

All your Lambda functions are run and handled by the AWS infrastructure which implies that you don’t need to worry about the underlying infrastructural requirements. This results in savings on operations on the network layer management and operating system upgrades.

4) Automatic Scalability

AWS Lambda generates instances of the functions based on the requests made. There are no scalability issues as the serverless nature allows it to accommodate unexpected increases. Every aspect is managed by itself and you only need to pay for the usage.

5) Integration with Products

AWS Lambda being on the platform of AWS integrates with many services like DynamoDB, S3, API gateway, and many more to create functional applications.

How to Schedule AWS Lambda Cron Jobs

Prerequisites:

  • S3 Bucket: The lambda function that would be used for the AWS Lambda cron jabs schedule will be in the form of a “deployment package” which is basically is .zip file that needs to be uploaded to the cloud through an S3 bucket.
  • IAM Role: All the privileges for the lambda functions need to be managed using IAM. also, the access to CloudWatch Logs also needs to be monitored. 
  • CloudWatch Events Rule: CloudWatch Events support the expressions which are used to define the creation of an event.
  • Lambda Permission: After the creation of the lambda function, permissions for invoking the functions are also important, and these permissions need to be provided explicitly.

NOTE: Amazon Web Services usually takes care of all the aspects. For this execution, we will be using the default values.

Step 1: Creating the Lambda Function

We will create a function that just prints the word “Hello, Welcome to Hevo” every 5 minutes. We will use the serverless instead of CloudWatch templates. 

  • Install serverless and Serverless CLI and configure the AWS credentials.
  • Create a Node.Js service.
  • A service in Serverless is a project which may consist of multiple lambda functions, events, triggers, and connections to other AWS resources.

Use the below code to create a serverless template for the AWS Lambda Cron job

serverless create --template "aws-nodejs" --path my-service
  • This will create a serverless.yml config file. Along with it is a handler.js file where the handler for the lambda function will be defined. 
  • Replace the contents of the handler.js file with the below code.
module.exports.hello = (event, context, callback) => {
  console.log("Hello, Welcome to Hevo")
  callback(null)
}
  • After the handler.js file is ready with the lambda function, the next step is to create and prepare the serverless.yml file.
  • Use the below code to replace the contents of the serverless.yml file:
service: hevo-service
provider:
  name: aws
  region: asia-central-1
  runtime: nodejs6.10
functions:
  hello:
    handler: handler.hello
    events:
      - schedule:
          rate: cron(*/5 * * * ? *)
          enabled: true

The parameters are mostly self-explanatory and require very less background to have a gist of the operations. The Scheduling of AWS Lambda Cron is complete.

What Makes Hevo’s ETL Process Best-In-Class

Providing a high-quality ETL solution can be a difficult task if you have a large volume of data. Hevo’s Automated, No-Code Platform empowers you with everything you need to have for a smooth data replication experience.

Check out what makes Hevo amazing:

  • Fully Managed: Hevo requires no management and maintenance as it is a fully automated platform.
  • Data Transformation: Hevo provides a simple interface to perfect, modify, and enrich the data you want to transfer.
  • Faster Insight Generation: Hevo offers near real-time data replication so you have access to real-time insight generation and faster decision making. 
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Scalable Infrastructure: Hevo has in-built integrations for 100+ Data Sources (with 40+ free sources) that can help you scale your data infrastructure as required.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-Day Free Trial!

Step 2: Deploy the Serverless package

Serverless manages the permissions and cofigures the IAM roles and the lambda permissions. It also creates packages for the deployment of AWS Lambda Cron, that allow the codes to be uploaded onto the S3 and you can deploy everything with a single command:

serverless deploy

Step 3: Verify the deployment of the Serverless package

  • After the AWS Lambda Cron job is deployed, the logs can be viewed using the command:
serverless logs --function hello --tail
  • This command helps in determining if the “Hello, Welcome to Hevo” has appeared every 5 minutes and the AWS Lambda Cron Job is running.
  • The output may look like this:
2022-04-11 21:00:22.173 (+05:30)        1b2ca513-a87d-12e7-a4f4-5ba657dec6d6    Hello, Welcome to Hevo
END RequestId: 1b2ca513-a87d-12e7-a4f4-5ba657dec6d6
REPORT RequestId: 1b2ca513-a87d-12e7-a4f4-5ba657dec6d6  Duration: 2.50 ms       Billed Duration: 100 ms         Memory Size: 1024 MB    Max Memory Used: 20 MB

This shows that the lambda function is running like an AWS Lambda cron function on a schedule. This function works on the cloud and is theoretically free as the AWS Lambda provides 1M free requests per month.

Steps to Shut Down the AWS Lambda cron

  • To shut down an AWS Lambda cron job just change the enabled = true flag to false and rerun the serverless deploy. You can also use serverless remove to completely remove the stack.
  • From the above example:
service: hevo-service
provider:
  name: aws
  region: asia-central-1
  runtime: nodejs6.10
functions:
  hello:
    handler: handler.hello
    events:
      - schedule:
          rate: cron(*/5 * * * ? *)
          enabled: false

or

serverless remove

Conclusion

Cron is a tool that helps in scheduling tasks on a Linux operating system. Cron jobs are automated jobs that are scheduled to work at a particular time frame. AWS Lambda is a serverless computing solution. scheduling jobs on this platform results in running the AWS Lambda Cron Jobs. In this article, a step-by-step guide is provided to schedule Cron Jobs.

There are various trusted sources that companies use as it provides many benefits but transferring data from it into a data warehouse is a hectic task. The Automated data pipeline helps in solving this issue and this is where Hevo comes into the picture. Hevo Data is a No-code Data Pipeline and has awesome 100+ pre-built Integrations that you can choose from.

visit our website to explore hevo[/hevoButton]

Hevo can help you Integrate your data from 100+ data sources and load them into a destination to Analyze real-time data. It will make your life easier and data migration hassle-free. It is user-friendly, reliable, and secure.

SIGN UP for a 14-day free trial and see the difference!

Share your experience of learning about AWS Lambda Cron Jobs in the comments section below.

Data Engineering
Survey 2022
Calling all data engineers – fill out this short survey to help us build an industry report for our data engineering community.
TAKE THE SURVEY
Amazon Gift Cards of $25 each are on offer for all valid and complete survey submissions.