Schedule AWS Lambda Function for Periodic Execution or Scheduled Lambda Functions
In this AWS tutorial for cloud developers, I want to show how to create scheduled AWS Lambda function to run periodically. Using Amazon CloudWatch Rules it is easy to trigger an AWS Lambda function. I will create a basic serverless Lambda function and schedule it using CloudWatch rules to execute the function periodically.
Create AWS Lambda Function
When you logged on to your account at AWS Management Console, launch AWS Management Console.
On Functions list page, press "Create function" to create a new Lambda function.
For simplicity of the AWS Lambda function, I will continue to create serverless function from scratch.
In basic information section, I entered following details.
Then press "Create function" button which is at the bottom of the page
On Configuration tab, in "Function code" section, replace the existing Python 3.7 code with following in the inline code editor.
Click "Save" button at to top right corner of the screen.
Then we are ready to test our sample AWS Lambda function.
Press "Test" button.
Type a descriptive test name. Since for this tutorial, we don't expect any incoming message or event, clear content in the textbox.
Press Create to save our test event for the sample AWS Lambda function
Press Test button once more. The Lambda function will be called with the test event we have save right now. You should be able to see a successfull execution result similar to following output.
Now we are ready to schedule the execution of this AWS serverless Lambda function using Amazon CloudWatch service.
Create CloudWatch Event Rule for Scheduled Lambda Execution
From Services on AWS Management Console, launch Amazon CloudWatch service dashboard.
Let's create a new rule. Start by pressing "Create rule" button.
Instead of default selected option "Event Pattern", choose option "Schedule"
As you can see in following screenshot, you can write your own Cron expression or as I did below I scheduled event source to trigger every minute.
In Targets section, press "Add target" button.
From the dopdownlist, choose Lambda Function then choose your AWS function we have created in previous step.
Press "Configure details"
Press Create rule
Since the rule is enabled by default, our AWS Lambda function will be triggered each minute.
To summarize, cloud developers using AWS as their cloud platform can trigger an AWS Lambda function periodically using CloudWatch events.
It is possible to monitor serverless function execution using Monitoring tab displayed at related AWS Lambda function details page.
We can monitor the scheduled execution using CloudWatch log groups for scheduled lambda function.
It is easy to jump from AWS Lambda function monitoring page to CloudWatch logs by using the "View logs in CloudWatch" button.
You see the AWS Lambda function is triggered and executed every minute periodically.
If you have "print()" commands in your AWS Lambda function Python code, these messages will be displayed in CloudWatch logs.
If you preferred to build your Lambda function code using Node.js instead of Python then you can use console.log() statement to write to Amazon CloudWatch logs.
I tried to demonstrate how cloud developers can create scheduled AWS lambda functions or create a schedule for AWS serverless Lambda functions to trigger them periodically using Amazon CloudWatch event rules.