CyberKeeda In Social Media

AWS Cloudwatch : How to create CloudWatch Rule and Schedule Lambda Function Using it.

AWS CloudWatch

Amazon CloudWatch is a monitoring and management service that provides data and actionable insights for AWS, hybrid, and on-premises applications and infrastructure resources. With CloudWatch, you can collect and access all your performance and operational data in form of logs and metrics from a single platform.

CloudWatch Rule : Create rules to invoke Targets based on Events happening in your AWS environment.

So this tutorial is just a walk through to
  • How to create a Cloudwatch rule using AWS Console
  • How to create a Cloudwatch rule and invoke lambda function using Rule scheduler and Cron expression.
  • What is Constant Keys and what is it's use while we use Lambda function as Target.

What we are trying to achieve here : 
  • We have a Lambda function named as "Check_File" and it's function is to check existence of file.
  • File is dynamic type : Format --> YYMMDDYdemo-file-A.txt
  • Thus this part of file name demo-file-A.txt  is treated as file_suffix.
  • So we will be using Constant keys to send the required value, which will be used as EVENTS within our Lambda Function.
  • Our dynamic file gets uploaded everyday between 11:30 to 12:00, thus we will trigger Lambda Function via Cloudwatch rule with Custom cron to check file existence.
Lab Setup details

  • AWS Lambda Function name : Check_File
  • AWS Lambda Function Task : Checks existence of file uploaded daily between 11:30 to 12:00
  • Cron Timing : Every Five minute between 11:30 to 12:00 GMT
  • Constant Keys : { "bucket_sub_directory": "reports/", "file_suffix": "demo-file-A.txt", "bucket_name": "cyberkeeda-bucket-a", "fileType": "daily"}

Steps to create Cloudwatch Rule via AWS Console.

  • AWS Console --> Cloudwatch --> Rules --> Create Rule
  • Enter the required details.
    • Event Source : 
      • Schedule --> Custom Cron --> 30,35,40,45,50,55,59 11 ? * * *
      •  Target --> Lambda Function --> Check_File
      • Constant Keys --> { "bucket_sub_directory": "reports/", "file_suffix": "demo-file-A.txt", "bucket_name": "cyberkeeda-bucket-a", "fileType": "daily"}
  • Configure Rule details :
    • Name : Cloudwatch Rule name
    • Description : Describe about it and Click on Create Rule.
Note : Replace the above Inputs with your own.

    We are done with the creation of Cloudwatch Rule via Admin Console.

Verify Cloudwatch logs to confirm if scheduled lambda executed.

Cloudwatch Rule via CloudFormation Script.

Use the below AWS CFN template to create above cloudwatch rule via AWS Cloudformation.

AWSTemplateFormatVersion: 2010-09-09
DescriptionCFN Script to create Lambda Function and a CloudWatch Rule with cron to trigger it.
    DescriptionProvide Lambda Function ARN Parameter. [a-z][a-z0-9]* 
    DescriptionProvide Cloudwatch Rule name. [a-z][a-z0-9]*

      Description!Sub "Scheduled Rule created to invoke Lambda Function: ${LambdaFunctionNameARNParam}"
      Name!Sub ${CloudwatchRuleNameParm}
      ScheduleExpressioncron(0/5 11-12 ? * * *)

      - IdLambda1
        Arn!Sub ${LambdaFunctionNameARNParam}
        Input'{ "bucket_sub_directory": "reports/",
                   "file_suffix": "demo-file-A.txt",                    "bucket_name": "cyberkeeda-bucket-a",
                   "fileType": "random"

      FunctionName!Sub ${LambdaFunctionNameARNParam}
      SourceArn!GetAtt ScheduledRule.Arn

No comments:

Post a Comment

Designed By Jackuna