Lambda functions can be triggered based on the events either synchronously and asynchronously.

In this guide , We will learn how to trigger lambda functions using the Cloudwatch events.

If you’re new to lambda function , And Want to know more about Lambda function and How to Create Lambda function using AWS Console, Check this article.

Invoking Lambda Functions

AWS Lambda functions can be invoked directly using the AWS Console , AWS CLI , AWS SDK and Lambda API.

It can also be invoked by other AWS services.

Below is the Lists of services that Invoke lambda functions Synchornously.

  • Application Load balancer
  • Alexa
  • API Gateway
  • Cloudfront
  • Kinesis Data firehose
  • Cognito
  • Amazon Lex
lambda

The lambda function will be executed immediately using the synchronous method and through the Lambda API calls and using aws cli.

Below is the Lists of services that Invoke lambda functions ASynchornously.

  • Simple Storage Service (S3)
  • Simple Notification Service (SNS)
  • Simple Email Service (SES)
  • Cloudformation
  • Cloudwatch Logs
  • Cloudwatch Events
  • AWS Config
  • AWS CodeCommit
lambda

When we trigger lambda function using this method , In case of any failure or errors during execution , It will retry twice.

Below is the lists of services that Invoke lambda functions based on Poll

  • Amazon Kinesis
  • DynamoDB Streams
  • Simple Queue Service (SQS)
lambda

This method invokes the functions synchronously by integrating with the above services.

For example , Lambda will retrieve the records from the Kinesis streams , check for any changes and trigger the lambda fucntion.

Setup Cloudwatch Event Rule

Lets create a cloudwatch event rule to invoke the lambda functions.

Login to Cloudwatch Console , In the left pane , Under Events , Choose Rules

Click Create rule , For the Event Source , Choose Schedule.

We are going to trigger the lambda function based on the Schedule by configuring the Cron expression.

You can either trigger the lambda function for the fixed rate of ‘n’ minutes.

Or You can configure specific Cron expression. For example , Everyday at 1 AM (0 1 * * *)

lambda

Next We have to add the Lambda function as a Target to this Event Rule.

Under Targets , Click Add target , In the drop down menu , Choose the Lambda function ,

For Function , Choose the function name.

lambda

If you want to trigger multiple lambda functions at the same time using the same Event Rule , You can add targets here.

Choose Configure details , Under the Rule definition , Specify a name for the rule with a short description.

Make sure that the State is Enabled.

lambda

Cloudwatch Events will automatically add a necessary permissions to trigger the lambda functions.

Verifying the Cloudwatch Event Rule

To verify whether the configured rule is working as expected , Go to Cloudwatch Console

Under Events , Choose Rules , Select the created rule ,

In Monitoring , Click Show metric for the rule , You should see the metrics graph for the rule as per the con expression.

Alternatively , You can also check the output of the Lambda function using the Cloudwatch Logs

Whenever the lambda function is invoked It outputs the results to the lambda function for any messages or errors.

From the Cloudwatch Console , Choose Logs , Click Log group

You can search for the Log group for that lambda function as shown below,

/aws/lambda/functionname

Inside you can see the lists of streams created for all the lambda executions.

Click the Latest stream to check the response of the lambda function.

Conclusion

We have understood the different methods of Lambda invokations , Also We have configured cloudwatch event rule to trigger the lambda function based on the cron expression or fixed rates.

Hope you found it helpful.

Thanks for reading this article.Please do check out my other publications.