SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for AWS Amazon Web Services, Redshift, AWS Lambda Functions, S3 Buckets, VPC, EC2, IAM

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.

create new AWS 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.

basic information about serverless Lambda function

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.

import json
import datetime

def lambda_handler(event, context):
 currentDT = datetime.datetime.now()
 print(str(currentDT))
 return {
  'statusCode': 200,
  'body': json.dumps(str(currentDT))
 }
Code

scheduled Lambda function code in Python

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.

test AWS Lambda function code

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.

test execution result of AWS Lambda function

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.

Amazon CloudWatch event rules to trigger AWS Lambda function

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.

CloudWatch event source to call Lambda function periodically

In Targets section, press "Add target" button.
From the dopdownlist, choose Lambda Function then choose your AWS function we have created in previous step.

trigger AWS Lambda function with CloudWatch events

Press "Configure details"

CloudWatch event rule for scheduled Lambda function calls

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.

monitor AWS Lambda function execution

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.

Amazon CloudWatch logs for scheduled AWS Lambda function calls

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.



AWS


Copyright © 2004 - 2024 Eralper YILMAZ. All rights reserved.