Guided Lab: Creating an Amazon EventBridge RDS DB Instance Event Rule
Description
Amazon EventBridge is a serverless event bus that allows you to respond to real-time changes in your AWS environment. By creating an EventBridge rule for Amazon RDS (Relational Database Service), you can automate actions based on RDS events, such as instance status changes, backups, or maintenance. This setup is particularly useful for automating monitoring, notifications, and recovery processes in your database operations.
In this lab, you will learn how to create an EventBridge rule to capture specific Amazon RDS DB instance events and trigger a target, such as an AWS Lambda function.
Prerequisites
This lab assumes you have the following:
- Basic knowledge and understanding of Amazon EventBridge, Amazon RDS and AWS Lambda.
If you find any gaps in your knowledge, consider taking the following lab:
- Creating an AWS Lambda function
- Getting Started with Amazon EventBridge Events
- Creating an Amazon RDS database
Objectives
By the end of this lab, you will:
- Understand how to configure an Amazon EventBridge rule for RDS events.
- Learn how to set a target for the event rule, such as an AWS Lambda function, to automate actions based on RDS events.
Lab Steps
Create an Amazon RDS Instance
1. Navigate to the RDS Console.
2. Create a Database with the following configuration:
- Choose a database creation method: Standard create
- Engine options:
- Engine type: MySQL
- Engine Version: Leave it as default
- Templates: Free tier
- Settings:
- DB instance identifier: database-1
- Master username: admin (you can change this as you desire)
- Credentials management: Self managed
- For the simplicity of this lab, we will tick the auto-generate password checkbox.
But take note that in PRODUCTION, you need to create a strong password for the security of your Database
- Instance configuration:
- DB instance size:, db.t3.micro
- Storage:
- Storage type: General Purpose SSD (gp2)
- Allocated storage: 20
- Leave the rest as default and click Create Database.
3. Wait for the RDs Database creation to finish. This will take a few minutes to finish.
Create an AWS Lambda Function (Target for the Event Rule)
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
- Select Use an Existing Role:
-
Click Create function
3. Replace the existing code with the following:
Note: We are using the old console editor for this lab. You can switch to the new editor as you desire; the process remains the same, but the interface may look slightly different.
import json
def lambda_handler(event, context):
print("RDS Event received:", json.dumps(event))
return {
'statusCode': 200,
'body': 'Event processed successfully'
}
The code defines a simple AWS Lambda function that logs Amazon RDS events when triggered by EventBridge. It starts by importing the json
module, which helps format the event data for logging. The main function, lambda_handler
, receives the RDS event information (contained in event
) and logs it using print
. This log appears in CloudWatch, allowing you to see details about the RDS event, such as the event type and which instance it involves. Finally, the function returns a response indicating that the event was successfully processed. This basic setup is helpful for monitoring and verifying that the event rule is working as expected.
4. Click Deploy to save the changes.
Create an Amazon EventBridge Rule for RDS Events
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:
MyRDSDBEventRule
- Description: Rule to capture RDS DB instance events
- Rule type: Rule with an event pattern
- Event Source: Select AWS services
- Service Name: Choose Relational Database Service (RDS)
- Event Type: Select RDS DB Instance Events
- Click Next to proceed.
5. Set the Target for the Rule
- For the Target type, select AWS service
- Under Select target, choose Lambda function.
- In the Function dropdown, select
myLambdaFunction
.
6. Click on Skip to Review and create. Take your time to double-check all the entered details carefully.
7. Scroll down and click Create rule.
Test the Rule
1. Go back to the RDS Console and perform an action that triggers an RDS DB instance event, such as stopping the instance temporarily.
- A confirmation message will appear; tick the Acknowledgement checkbox and click on Stop temporarily.
2. Wait for it to finish. This will take a few minutes to finish.
- A notification will appear once the process is complete.
3. Navigate to the Lambda Function (myLambdaFunction) created earlier. Then, click on the Monitor tab and click on View CloudWatch logs.
- You will be redirected to the CloudWatch logs for this lambda function. Scroll down and view the latest log stream. Check the log stream to see the event details.
Congratulations! You have successfully created an Amazon EventBridge rule to capture RDS DB instance events and trigger a Lambda function. This setup is useful for automating actions responding to database events, such as sending notifications or executing recovery processes.
Key Takeaways:
- EventBridge makes it easy to automate actions based on real-time changes in your AWS environment.
- Integrating RDS with EventBridge allows for proactive monitoring and management of database events.
- Using Lambda as a target enables serverless processing and automation.
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!!!