Serverless computing, while still nascent, is growing in popularity. Hiding the deployment infrastructure from the developer, lets companies do what they do best – develop products. Serverless deployments lower administrative overhead, reduce cost, and increase scalability.
In this tutorial, we deep dive into setting cron jobs on AWS Lambda with CloudWatch events to make it Serverless.
Typical use case
Let’s look at the use case:
We need to fetch data from social networks and save it into the database/Elasticsearch. A similar use case could be to send an email every morning for a specific event.
I decided to use Claudia.js – a tool that automates the deployment of AWS Lambda and API Gateway configurations. AWS CLI was used to run the cron job on AWS Lambda with the Cloudwatch event.
Before we get to the task of creating the cron job, we need to create a Node.js app or service using Express. We then make this app or service Serverless and lastly, we need to attach the cron job to it.
The entire build is a three-part process:
- Build a Node.js application using the Express framework
- Make it Serverless by deploying AWS Lambda using Claudia
- Use AWS CLI to run the cron job on AWS Lambda with the CloudWatch event
Build Node.js application using the Express framework
Here is a step-by-step tutorial to create a “Hello world” application, using NodeJs and Express framework.
Create a folder with any name. Within that folder, create an index.js file with the following code snippet in it.
Create a Node.js app using the npm init -y command on your terminal. You must ensure that the active folder is the one that contains the index.js file.
Run npm install express from your terminal to install the Express Web Framework.
Run node index.js and visit http://localhost:3000 in your browser. You should now see the “Hello world!” message.
Make it Serverless by deploying AWS Lambda using Claudia
This section focuses on deploying an existing Node.js app on AWS Lambda using Claudia. Click here for Claudia’s documentation. As is obvious, a Serverless API consists of AWS Lambda and Gateway.
To deploy and make the existing Node.js “Hello World!” app into a Serverless API, we need to modify the current index.js file in such a way that it supports AWS Lambda and the deployment using Claudia.
Our modified index.js file is as follows:
Let’s run the application on a local machine and create another file with the name index-local.js. This enables you to run your Node.js app from a local machine using the command node index-local.js.
Before running Claudia on your terminal you must have access to your AWS access credentials. We now install Claudia in our Node.js app (locally) by running npm install claudia. To run Claudia globally, use the command npm install -g claudia. See here for more information on installing Claudia.
Create a wrapper for AWS Lambda
Next, we generate a wrapper function to get the Node.js app working with AWS Lambda. This function already exists in Claudia and can be generated using this command:
claudia generate-serverless-express-proxy –express-module index
In the above command, index is the name of the entry file without the .js extension. You should see the following screen:
claudia create –handler lambda.handler –deploy-proxy-api –region us-east-2 –memory 512 –timeout 30
- the ‘region’ parameter is used to specify the region where you wish to create your Lambda resource
- the ‘memory’ parameter is used to specify the memory in MB. Memory should be a multiple of 64, and the default memory is 128MB
- the ‘timeout’ parameter is used to indicate the function execution time, in seconds
This command shows the following output after sending your Node.js app to Lambda:
Now, visit this URL listed in the JSON. You should see the “Hello World!” text in your browser.
Hurray, this works! This completes our Serverless deployment on AWS. Now let’s move on to the last part of running cron jobs on AWS Lambda.
Use AWS CLI to run the cron job on AWS Lambda with the CloudWatch event
Let’s consider, the following use case:
The Lambda function fetches data from different social networks and updates the database. It needs to schedule the events at a specific time each day. The function also sends an email to all employees at 8.00 AM every day and a text message to all users once every week.
Before moving forward ensure that you have installed AWS CLI. Click here for more information on how to do this.
If you observe, the index.js file contains a console.log(“Triggered lambda function at scheduled event”) statement. This enables us to identify an event in the AWS log after triggering the cron job on it.
Follow these commands to run the cron job on the “Hello World!” Lambda function:
To run the cron job or schedule the event after every 5 minutes, enter the following command on your terminal.
aws events put-rule –name helloWorldRule –schedule-expression ‘cron(0/5 * * * ? *)’
- the ‘name’ helloWorldRule – provides the name of the rule. This name can be set to anything and is up to the developer.
- the ‘schedule-expression’ ‘cron(0/5 * * * ? *)’ – this is a cron expression that will invoke every 5 minutes.
For more schedule expressions use this link.
The above command will show the following output:
Enter the following command, to invoke the AWS Lambda function based on the created rule.
aws lambda add-permission –statement-id hello-scheduled-message –action lambda:InvokeFunction –principal events.amazonaws.com –source-arn arn:aws:events:us-east-2:445420586144:rule/helloWorldRule –function-name nodecronjob –region us-east-2
- ‘statement-id’ – The hello-scheduled-message is the statement id
- ‘action’ – It is an action to invoke the Lambda function
- ‘source-arn’ – This is an Amazon Resource Name of the CloudWatch event rule received from the previous command
- ‘function-name’ – This is a function name that is available in the claudia.json file
- ‘region us-east-2’ – This is the region name also found from the claudia.json file
The above command provides the following JSON response:
These final commands set the event:
aws events put-targets –rule helloWorldRule –targets “Id”=”1″,”Arn”=”arn:aws:lambda:us-east-2:445420586144:function:nodecronjob”
Here, ‘ARN’ is the value from the previous command. This command shows the following output:
We have now successfully run and scheduled the cron job on the “Hello World!” Lambda function. How do we know if the cron job is active? The AWS logs would let us verify.
The logs show the trigger message in the console.log() statement and the time it triggered.
There are several ways to make Node.js + Express applications Serverless. However, I have tried to explain a way where results can be achieved in minimal time and commands for creating and invoking the function and deployment.
“Synerzip team is very responsive & quick to adopt new technologies. Team naturally follows best practices, does peer reviews and delivers quality output, thus exceeding client expectations.”
“Synerzip’s agile processes & daily scrums were very valuable, made communication & time zone issues work out successfully.”
“Synerzip’s flexible and responsible team grew to be an extension to the StepOne team. Typical concerns of time zone issues did not exist with Synerzip team.”
“Synerzip worked in perfect textbook Agile fashion – releasing working demos every two weeks. Though aggressive schedules, Synerzip was able to deliver a working product in 90 days, which helped Zimbra stand by their commitment to their customers.”
“Outstanding product delivery and exceptional project management, comes from DNA of Synerzip.”
“Studer product has practically taken a 180% turn from what it was, before Synerzip came in. Synerzip cost is very reasonable as compared to the work they do.”
“Synerzip makes the timezone differences work FOR the customer, enabling a positive experience for us. ‘Seeing is believing’, so we decided to give it a shot and the project was very successful.”
“The Synerzip team seamlessly integrates with our team. We started seeing results within the first sprint. And due to the team’s responsiveness, we were able to get our product to the sales cycle within 7 months.”
“Product management team from Synerzip is exceptional and has a clear understanding of Studer’s needs. Synerzip team gives consistent performance and never misses a deadline.”
“Synerzip is different because of the quality of their leadership, efficient team and clearly set methodologies. Studer gets high level of confidence from Synerzip along with significant cost advantage of almost 50%”
“Synerzip’s hiring approach and practices are worth applauding. Working with Synerzip is like
“What you see is what you get”.”
“Synerzip has dedicated experts for every area. Synerzip helped Tangoe save a lot of cost, still giving a very high quality product.”
“Synerzip gives tremendous cost advantage in terms of hiring and growing the team to be productive verses a readymade team. Synerzip is one company that delivers “co –development” to the core!”
“Synerzip is a great company to work with. Good leadership and a warm, welcoming attitude of the team are additional plus points.”
“Our relationship with Synerzip is very collaborative, and they are our true partners as our values match with theirs.”
“Synerzip has proven to be a great software product co-development partner. It is a leader because of its great culture, its history, and its employee retention policies. ExamSoft’s clients are happy with the product, and that’s how ExamSoft measures that all is going well.”
“They possess a great technical acumen with a burning desire to solve problems. The team always takes the initiative and ownership in all the processes they follow. Synerzip has played a vital role in our scaling up and was a perfect partner in cost, efficiency, and schedules.”
“As we are a startup, things change on a weekly basis, but Synerzip team has been flexible in adapting the same”
“Synerzip team has been very proactive in building the best quality software, bringing in best practices, and cutting edge innovation for our company.”
“We’ve been working for more than six years with Synerzip and its one of the better, if not the best, experience I’ve had working with an outsourcing company.”
“My experience with Synerzip is that they have the talent. You throw a problem at them, and someone from that team helps to solve the issue.”
“The breadth and depth of technical abilities that Synerzip brings on the table and the UX work done by them for this project exceeded my expectations!”
“Synerzip UX designers very closely represent their counterparts in the US in terms of their practice, how they tackle problems, and how they evangelize the value of UX.”
“Synerzip team understood the requirements well and documented them to make sure they understood them rightly.”
“Synerzip is definitely not a typical offshore company. Synerzip team is incredibly communicative, agile, and delivers on its commitments.”
“Working with Synerzip helped us accelerate our roadmap in ways we never thought possible!”
“While working with Synerzip, I get a feeling of working with a huge community of resources, who can jump in with the skills as needed.”