How to Schedule AWS Lambda functions using EventBridge

How to Schedule AWS Lambda functions using EventBridge

AWS, How to By Jun 20, 2022 No Comments

How to Schedule AWS Lambda functions using EventBridge

There are multiple rules from which you can choose to run AWS Lambda function on a schedule. This tutorial will help you in creating the rule with the help of AWS management console or the AWS CLI. You must install the AWS CLI on your system before using it. Visit Installing, updating, and uninstalling the AWS CLI version 2.

For Schedules, EventBridge doesn’t provide second-level precision in schedule expressions. One minute is the best resolution when using a cron expression. EventBridge possesses a distributed nature and the target services, a minor delay of a few seconds between the time the schedule is triggered and the time the target service runs the target resource is expected.

Step 1:- Creating a Lambda function

A lambda schedule is required to log the scheduled events.

1- Open the AWS Lambda console at https://console.aws.amazon.com/lambda/
2- Click on Create function
3- Click on Author from scratch.
4- Now you have to create a name along with a description for the Lambda function. For example, LogScheduledEvent.
5- Click on Create function by leaving the other options as default.
6- Now double click on index.js on the code tab of the function page.
7- Replace the existing code with the following code.

'use strict';

exports.handler = (event, context, callback) => {
    console.log('LogScheduledEvent');
    console.log('Received event:', JSON.stringify(event, null, 2));
    callback(null, 'Finished');
};

Step 2: Creating a Rule

A rule is to be created for running the Lambda function same like you created in step 1 on a schedule.

The management console or an AWS CLI can be used for creating a rule in this step. You must grant rule permission before stepping ahead to invoke your Lambda function. After that, you are able to create the rule and add the Lambda function as a target.

By using console-

1- Open the Amazon EventBridge console at https://console.aws.amazon.com/events/
2- Click on rules in the Navigation pane.
3- Click on Create rule.
4- Now, mention the name and description for this rule. A rule can’t have the same as another rule in the same Region and on the same event bus.
5- For the event bus, choose the event bus that you want to associate with this rule. If you want this rule to match events that come from your account, select AWS default event bus. When an AWS service in your account emits an event, it always goes to your account’s default event bus.
6- Click on Schedule for Rule type.
7- Now, click on Next.
8- Click on A schedule which runs at a regular rate, like every 10 minutes for Schedule pattern. Enter 5 and click on Minutes from the drop-down list.
9- Click on Next.
10- Click on AWS service for Target types.
11- Click on Lambda function in the drop-down list for the purpose of selecting a target.
12- For Function, select the Lambda function that you made in Step 1: Creating a Lambda function section. For example, choose LogScheduledEvent.
13- Click on Next.
14- Click on Next.
15- Click on Create Rule after reviewing and re-checking the other details and steps followed above.

By Using AWS CLI-

1- Use the put-rule command for preparing the rule that runs on a schedule.

aws events put-rule \
--name my-scheduled-rule \
--schedule-expression 'rate(5 minutes)'

When running, this rule produces an event and then sends it to the targets. The following is an example event.

{
    "version": "0",
    "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
    "detail-type": "Scheduled Event",
    "source": "aws.events",
    "account": "123456789012",
    "time": "2015-10-08T16:53:06Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule"
    ],
    "detail": {}
}

2- Make use of add-permission command to grant permission to the EventBridge service principal (events.amazonaws.com) to run the rule.

aws lambda add-permission \
--function-name LogScheduledEvent \
--statement-id my-scheduled-event \
--action 'lambda:InvokeFunction' \
--principal events.amazonaws.com \
--source-arn arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule

3- Enter the put-targets command for adding the Lambda function created in step 1.

aws events put-targets --rule my-scheduled-rule --targets file://targets.json

4- Create the file targets.json with the following contents.

[
  {
    "Id": "1", 
    "Arn": "arn:aws:lambda:us-east-1:123456789012:function:LogScheduledEvent"
  }
]

Step 3: Verifying the rule

After finishing Step 2, Wait for five minutes and then start the process Verifying your Lambda function. Whether it is invoked or not.

View the output from your Lambda function

1- Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/
2- Click on Logs in the Navigation Pane.
3- Select the name of the log group for your Lambda function (/aws/lambda/ function-name).
4- Select the name of the log stream to view the data provided by the function for the instance that you launched.

Step 4: Confirming Success

Now, if you are able to see the Lambda event CloudWatch logs then you have performed this tutorial successfully. But, if the Lambda event is not showing in your CloudWatch logs then start the process of troubleshooting by verifying the rule was created successfully and, if the rule looks correct, verify if the code of your Lambda function is correct or not.

Step 5: Cleaning up your resources

You can retain the resources used in this tutorial if you want otherwise you are free to delete all of them. You will be exempted from any unnecessary charges by your AWS account by deleting them.

Process of deleting the EventBridge rules-

1- Open the Rules page of the EventBridge console.
2- Click on the Rule(s) that you created.
3- Click on Delete.
4- Click on Delete.

Process of deleting the Lambda Function(s)-

1- Open the Functions page of the Lambda console.
2- Click on the functions that you created.
3- Choose Actions, Click on delete.
4- Click Delete.

Author

I'm Abhay Singh, an Architect with 9 Years of It experience. AWS Certified Solutions Architect.

No Comments

Leave a comment

Your email address will not be published. Required fields are marked *