Create a Cron Job on AWS Lambda

3D ad banner ad revenue ad revenues adobe XD Advanced agency Agile Agile Development Ajax AlexW analytics Angelap angular AngularJS Animation anime.js api apollo Appsflyer Async async components Avocode AWS AWS Lambda axios b2b b2c baas Banner banner ad Bannersnack bawmedia bem Blogs bootstrap browser testing build process bundler Business business success Canvas & SVG career Career Advancement charting chat app chatbots ChatKit chrome chrome extension chrome extensions ciphering CloudFlare cms code challenge ColinI components content context api cron job cross browser testing cross platform development cross-browser CSS CSS Architecture css challenge css framework css frameworks css grid css modules cssbattle.dev D3.js data visualization databases decoupled cms deep linking Design Design & UX design career design handoff dev environment DevOps diagnostic analytics digital agency digitalocean E-commerce edge computing Entrepreneur Entrepreneurship es6 eslint Expo Express FaaS Feathers Feathers.js figma framework Frameworks freelancing functions gatsby generators gitCS github API GraphCMS graphing graphql Growth Grunt Gulp headless CMS hooks HTML HTML & CSS html preprocessor HTML5 https://www.sitepoint.com/windows-linux-subsystem-2-windows-terminal IAAS Illustration ionic jade jamesh JavaScript javascript framework javascript frameworks Jest joelf jQuery jquery4u lambdatest launch checklist Layout Learn-Node-JS learn-vue linux location tracking Managed marvel MBaaS mekanism mind map mind mapping mindmapping Mobile mobile attribution Mobile Design modernjs modernjs-hub modernjs-tools monday mongodb multi-threading nest nilsonj node node version manager node-hub Node-JS-Tools Node.js nodejs noops npm octobercms online publishing PaaS Package Management package manager Percy preprocessors prettier product design Profitability Programming PubNub Pug Pusher Quasar quote builders Raw JavaScript React React Hooks React native Reat native elements Recurring Payments redux Resources restful api Review SaaS sass self sencha serverless serverless computing sharing state side project siteground sketch slashdata Software specifications sponsored state state management static module bundler StudioWorks Styled Components survey svelte sympli Tailwind task runner telegram bots Template engine Testing Themes Tinder Tools & Libraries TypeScript Ubuntu UI Design Usability user experience user research UX ux issues visual testing vps vue Vue CLI vue dev environment vue-hub vue-tutorials vue.js vuetify vuex WatermelonDB Web Web Design web hosting Web Hosting & Domains Web standards web workers webpack website builders WebSockets windows Windows Subsystem for Linux Windows Terminal women women in tech WordPress wordpress themes work workers workflow WPEngine WSL WSL2 Zeplin

Create a Cron Job on AWS Lambda

Cron jobs are really useful tools in any Linux or Unix-like operating systems. They allow us to schedule scripts to be executed periodically. Their flexibility makes them ideal for repetitive tasks like backups and system cleaning, but also data fetching and data processing.

For all the good things they offer, cron jobs also have some downsides. The main one is that you need a dedicated server or a computer that runs pretty much 24/7. Most of us don't have that luxury. For those of us who don't have access to a machine like that, AWS Lambda is the perfect solution.

AWS Lambda is an event-driven, serverless computing platform that's a part of the Amazon Web Services. It’s a computing service that runs code in response to events and automatically manages the computing resources required by that code. Not only is it available to run our jobs 24/7, but it also automatically allocates the resources needed for them.

Setting up a Lambda in AWS involves more than just implementing a couple of functions and hoping they run periodically. To get them up and running, several services need to be configured first and need to work together. In this tutorial, we'll first go through all the services we'll need to set up, and then we'll implement a cron job that will fetch some updated cryptocurrency prices.

Understanding the Basics

As we said earlier, some AWS services need to work together in order for our Lambda function to work as a cron job. Let's have a look at each one of them and understand their role in the infrastructure.

S3 Bucket

An Amazon S3 bucket is a public cloud storage resource available in Amazon Web Services' (AWS) Simple Storage Service (S3), an object storage offering. Amazon S3 buckets, which are similar to file folders, store objects, which consist of data and its descriptive metadata. — TechTarget

Every Lambda function needs to be prepared as a “deployment package”. The deployment package is a .zip file consisting of the code and any dependencies that code might need. That .zip file can then be uploaded via the web console or located in an S3 bucket.

IAM Role

An IAM role is an IAM identity that you can create in your account that has specific permissions. An IAM role is similar to an IAM user, in that it is an AWS identity with permission policies that determine what the identity can and cannot do in AWS. — Amazon

We’ll need to manage permissions for our Lambda function with IAM. At the very least it should be able to write logs, so it needs access to CloudWatch Logs. This is the bare minimum and we might need other permissions for our Lambda function. For more information, the AWS Lambda permissions page has all the information needed.

CloudWatch Events Rule

CloudWatch Events support cron-like expressions, which we can use to define how often an event is created. We'll also need to make sure that we add our Lambda function as a target for those events.

Lambda Permission

Creating the events and targeting the Lambda function isn’t enough. We'll also need to make sure that the events are allowed to invoke our Lambda function. Anything that wants to invoke a Lambda function needs to have explicit permission to do that.

These are the building blocks of our AWS Lambda cron job. Now that we have an idea of all the moving parts of our job, let's see how we can implement it on AWS.

Implementing a Cron Job on AWS

A lot of the interactions we described earlier are taken care of by Amazon automatically. In a nutshell, all we need to do is to implement our service (the actual lambda function) and add rules to it (how often and how the lambda will be executed). Both permissions and roles are taken care of by Amazon; the defaults provided by Amazon are the ones we'll be using.

Lambda function

First, let's start by implementing a very simple lambda function. In the AWS dashboard, use the Find Services function to search by lambda. In the lambda console, select Create a function. At this point, we should be in Lambda > Functions > reate Function.

To get things going, let's start with a static log message. Our service will only be a print function. For this, we'll use Node.js 10x as our runtime language. Give it a function name, and on Execution Role let's stay with Create a new role with basic lambda permissions. This is a basic set of permissions on IAM that will allow us to upload logs to Amazon CloudWatch logs. Click Create Function.

Create a new lambda function

Our function is now created with an IAM Role. In the code box, substitute the default code with the following:

exports.handler = async (event) => {
    console.log("Hello Sitepoint Reader!");
    return {};
};

To check if the code is executing correctly, we can use the Test function. After giving a name to our test, it will execute the code and show its output in the Execution Result field just below our code.

If we test the code above we can see that we have no response, but in the function logs, we can see we have our message printed. This indicates that our service is running correctly so we can proceed with our cron implementation.

The post Create a Cron Job on AWS Lambda appeared first on SitePoint.

Original Article

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.