Skip to content

Easy way to create scheduled tasks on AWS serverless platforms



Hi folks,

In this era of Serverless Technologies, I would like to share how easy create a scheduled task based on AWS lambda functions and CloudWatch.

prerequisite : you have to create AWS account beforehand, AWS offer free-tier program that good enough if you want to explore all of AWS services. More detail about AWS free-tier program

AWS Lambda, lets you run code without provisioning or managing servers. More detail about AWS Lambda

Amazon Cloudwatch, is a monitoring service for AWS cloud resources and the applications you run on AWS. You can use Amazon CloudWatch to collect and track metrics, collect and monitor log files, set alarms, and automatically react to changes in your AWS resources. More detail about Amazon Cloudwatch

On this tutorial, we will demonstrate how to run a lambda function periodically. Let’s rock!

1. First step, let’s create a lambda function.

Login into AWS console, on the home page choose Lambda menu (under Compute parent menu).

Then, create a Lambda function. Choose blank function on the select blueprint steps. Then, just click next on configure triggers steps. Then, you will arrive in configure function section.  Fill the name with lambda-cron-fn, (optional) fill description of the function, and then select the runtime (we will use NodeJS runtime at this moment). Then, on the code entry type choose Edit code inline. Write this simple code :

we create simple script to prove if the cron is running well, with logging string “cool, it’s running …”

On the lambda function handler and role, keep handler name that already filled with index.handler. On the role, choose “Create new role from template(s)” if you don’t have a role beforehand and fill the role name. Then klik Next to create the function.

2. Second steps, create CloudWatch events rules 

After finish creating the lambda function, then let’s create CloudWatch event rules that behave as a trigger for it’s lambda function. To deal with this, navigate to home page (or through menu Services on top left of the page) then select CloudWatch (under management tools parent menu). Navigate to Events -> Rules, then you will see the CloudWatch’s rules list.

Then, click Create Rule button.

Step 1, we define the even source. On this tutorial we only cover schedule as the event source. On the schedule event source, there’s two type of schedule. First form is Fixed rate, meaning we create fixed interval of schedule by choosing this option (the units is minutes, hours, and days). Second form is by using cron expression, we can use cron expression as usual as we use it on the -nix-family ( crontab -e ).

Step 2, still in the same page, click “Add target*” button, then choose “Lambda function”, then in the function textbox choose the lambda function that we have created previously (actually whenever you focus-in the cursor on it, it would be give suggestion/autocomplete that listing all available lambda functions), then choose the function.

Step3, Give a name for this rule and (optional) a description of this rule. Don’t forget to checklist State Enabled, if you un-checklist it, this rule won’t be activated. Then finish this step by clicking Create Rule.

Back to lambda function console, then navigate to monitoring tab, after the scheduled time is reached, we can see the statistic (Invocation count) will be increased.

And then, to prove whether the function is running well or not, we can see the log that we’ve created inside, throughCloudWatch logs, click link “View logs in CloudWatch”, then we will navigated to CloudWatch logs page.

Super, we can see the logs string that we’ve created “cool, it’s running …”

That’s all, is super easy create scheduled tasks that trigger a function on the AWS serverless stack.


It's only fair to share...Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0
Published inAWSAWS LambdaNodejsProgrammingServerless

Be First to Comment

Leave a Reply

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

− 1 = six