Automatically Trigger Lambda Functions

HOW TO TRIGGER LAMBDA FUNCTIONS AUTOMATICALLY

2 min Aug 16, 2021 Manish Kumar CLOUD Views : 3017
In this blog post, we will setup a Cloudwatch event using which we can trigger the lambda functions automatically on the regular time intervals. 

Lambda Functions Invocations

AWS Lambda functions can be invoked in many ways such as AWS Console, AWS CLI, AWS SDK and Lambda API.
There are other AWS services that can trigger the lambda function as follows.Services used to invoke lambda function Synchronously,
  1. Application Load balancer - ALB
  2. Alexa
  3. API Gateway
  4. CloudFront Distribution
  5. Kinesis Data firehose
  6. Cognito
  7. ​Amazon Lex
API-Gateway-1.png

Immediate invocation of lambda function can be performed using the Synchronous method
Below services are used for Asynchronous invocation of the Lambda function.

  1. Simple Storage Service (S3)
  2. Simple Notification Service (SNS)
  3. Simple Email Service (SES)
  4. CloudFormation
  5. Cloudwatch Logs
  6. Cloudwatch Events
  7. AWS Config
  8. ​AWS Code Commit

Cloudwatch-Event.png

Any execution failures or errors during Asynchronous invocation, there are two retry will be performed
Poll based invocation of lambda functions can be done using the below listed AWS services.

  1. Amazon Kinesis
  2. DynamoDB Streams
  3. Simple Queue Service (SQS)

Kinesis.png

Lambda will retrieve the information from the above listed services such as Kinesis streams and then if there are any changes then it will trigger the lambda function.

Creating Cloudwatch Event Rule

We will create and configure Cloudwatch event rule in order to trigger / invoke the lambda functions.

Login to Cloudwatch Console, 

On the left side, Below Events, Choose Rules

Choose Create rule, Under the Event Source, Select Schedule.

Let’s us configure the cloudwatch event rule to trigger the lambda function based on the Cron expression

The Lambda function can be triggered for the fixed rate of ‘n’ minutes or

Based on the Cron expressions. For example, Trigger every day at 1 AM (0 1 * * *)


                       Also read: How are Cloud Services transforming the Business World?
 

Event-Source.png

Next is to add the Lambda function as a Target.

Under Targets, Choose Add target, On the dropdown menu, select the preferred Lambda function,

For Function, Select the function name.

Targets.png


You can add multiple lambda functions under Targets, if you want to trigger all at once using the CloudWatch event rule

For Configure details, Under the Rule definition, enter a name for the rule and then provide a short description.

State should be Checked (Enabled).

Rule-Definition.png

Necessary permissions for the cloudwatch event rule will be automatically added to execute the Target lambda functions.

Verify The Cloud Watch Event Rule

To verify whether the cloudwatch event rule has triggered the Lambda functions based on the Cron expression.
Go to Cloudwatch Console.

Under Events, Select Rules, and then select the Event rule,

In Monitoring, Select Show metric for the rule. Based on the cron expression you can find the metrics for the event rule

And also, For every execution the lambda function logs information such as INFO or ERROR to Cloudwatch Log groups.

Hence To verify the execution of the lambda function,

In the Cloudwatch Console, Select Logs, Choose Log group

Search for the Log group of that lambda function as mentioned below,

/aws/lambda/functionname

All the executions will create a log stream and log the execution details there.

From the latest log stream we can find whether the lambda is executed successfully as per the event rule and we can find the logs for the same.

Conclusion:

We have learnt about the types of Lambda invocations. Then we have configured a cloudwatch event rule based on the Cron expressions to invoke/trigger the lambda functions.

                              You May Also Like: AWS vs Azure vs Google Cloud