Guided Lab: Creating an Amazon EventBridge Scheduled Rule
Description
Amazon EventBridge is a serverless event bus service that allows you to create rules to match and route events between AWS services and your applications. With EventBridge, you can set up scheduled rules to trigger actions at regular intervals or on specific dates and times. This functionality is useful for automating routine tasks, such as running periodic jobs or triggering Lambda functions to perform scheduled maintenance.
In this lab, you will learn how to create a scheduled rule in Amazon EventBridge that triggers an AWS Lambda function at a specified interval.
Prerequisites
This lab assumes you have the following:
- Basic knowledge and understanding of Amazon EventBridge, and AWS Lambda.
If you find any gaps in your knowledge, consider taking the following lab:
Objectives
By the end of this lab, you will:
- Understand how to create and configure a scheduled rule in Amazon EventBridge.
- Learn how to use a scheduled rule to trigger an AWS Lambda function.
Lab Steps
Create a Lambda Function
1. Navigate to the AWS Lambda Console.
2. Create a new Lambda function using the following configurations:
- Choose Author from scratch.
- Function name: myLambdaFunction
- Select Python 3.12 as the runtime.
- Execution role:
- Select Use an Existing Role: PlayCloud-Sanbox
- Click Create function
3. Replace the existing code with the following:
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
logger.info("Scheduled Event triggered")
return {
'statusCode': 200,
'body': 'Success'
}
The code logs a simple message when the scheduled rule triggers the Lambda function. This provides a basic way to confirm that the EventBridge rule works as expected.
4. Click Deploy to save the function.
Create an EventBridge Scheduled Rule
1. Navigate to the EventBridge Dashboard.
2. Click on Rules in the left navigation pane.
3. Click Create rule.
4. Configure the rule with the following details:
- Name:
MyScheduledRule
- Description: Rule to trigger Lambda function every minute
- Event Source: Select Schedule
- Click on Continue to create rule
- Click on Continue to create rule
5. Under Schedule pattern, choose A schedule that runs at a regular rate, such as every 10 minutes, and set Rate expression to 1 minute
(for testing purposes). You can customize this based on your requirements (e.g., 5 minutes
, 1 hour
, etc.).
6. Click Next.
7. Under Select target, choose AWS service and then select Lambda function.
- Under Function, choose
myLambdaFunction
.
7. Click Next until Step 5: Review and create. Review all the settings to ensure they are correct.
8. Scroll down and click Create rule to finish configuring the target.
Monitor Lambda Function Logs
1. Wait for a few minutes (depending on the rate you set for your scheduled rule, e.g., every 1 minute).
2. During this time, the EventBridge rule should trigger the Lambda function automatically based on the schedule.
3. Navigate to the Lambda Function (myLambdaFunction) created earlier. Then, click on the Monitor tab and click on View CloudWatch logs.
5. You will be redirected to the CloudWatch logs for this lambda function. Scroll down and view the latest log stream.
- You should see log streams that correspond to the recent executions of your Lambda function. Click on the latest log stream to view the logs.
- Confirm that you see the message:
"Scheduled Event triggered"
in the logs. This indicates that the Lambda function ran successfully and logged the message.
Congratulations! You have created an Amazon EventBridge scheduled rule that triggers a Lambda function every minute. Scheduled rules are powerful tools in AWS that allow you to automate tasks and manage event-driven workflows efficiently.
Key Takeaways:
- Amazon EventBridge enables you to schedule automated actions, simplifying routine maintenance and monitoring tasks.
- Using Lambda as a target for EventBridge rules provides a serverless way to execute tasks on a schedule.
- You can customize the frequency of the rule to meet your specific requirements.
As a final step, remember it’s always a best practice to clean up resources after completing a lab. Deleting unused resources will help maintain a clutter-free AWS environment.
Happy learning!!!