How to Trigger Lambda Functions Automatically
August 16, 2021
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,
- Application Load balancer - ALB
- API Gateway
- CloudFront Distribution
- Kinesis Data firehose
- Amazon Lex
Immediate invocation of lambda function can be performed using the Synchronous method
Below services are used for Asynchronous invocation of the Lambda function.
- Simple Storage Service (S3)
- Simple Notification Service (SNS)
- Simple Email Service (SES)
- Cloudwatch Logs
- Cloudwatch Events
- AWS Config
- AWS Code Commit
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.
- Amazon Kinesis
- DynamoDB Streams
- Simple Queue Service (SQS)
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?
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.
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).
Necessary permissions for the cloudwatch event rule will be automatically added to execute the Target lambda functions.
Verify the Cloudwatch 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,
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.
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.