Guided Lab: Creating an Amazon EventBridge DynamoDB Event Rule
Description
Amazon EventBridge is a serverless event bus service that allows you to respond to changes in your AWS resources. Using EventBridge with DynamoDB events allows you to create robust, event-driven applications that respond in real-time to various DynamoDB lifecycle events and changes. This integration is useful for several scenarios:
- Automated Resource Management: For example, automatically adjust permissions or configurations whenever a new table is created or removed.
- Change Notifications: Send notifications when specific changes occur to DynamoDB tables, such as alterations in backup policies or scaling events.
- Backup and Recovery Automation: Automatically trigger actions for creating backups or snapshots in response to specific DynamoDB events.
- Monitoring and Compliance: Enable logging or alerts to track table-level activities for audit or compliance purposes.
In this lab, you will learn how to create an EventBridge rule that captures events from a DynamoDB table and triggers a 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:
Objectives
By the end of this lab, you will:
- Learn how to configure an EventBridge rule for DynamoDB events.
- Set up a target, such as an AWS Lambda function, to automate actions based on the events.
Lab Steps
Create a DynamoDB Table
1. Navigate to the DynamoDB service in the AWS Management Console.
2. Create a new table with the following configurations:
- Table name: MyDynamoDBTable
- Primary key:
ID
(String) - Table settings: Select
Default settings
- Scroll down and click Create table
3. Ensure the table status is set to Active before proceeding to the next step.
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("DynamoDB Event received:", json.dumps(event))
return {
'statusCode': 200,
'body': 'Event processed successfully'
}
The Lambda function code is a basic setup to handle and log events from a DynamoDB table. It imports the json
module to format the event data and defines the lambda_handler
function automatically triggered by Amazon EventBridge when a DynamoDB event occurs. The event
parameter contains details about the DynamoDB events, such as turning on or off the deletion protection. The function logs this event data to Amazon CloudWatch for monitoring and debugging, making it easy to verify the events. Finally, the function returns a successful response, confirming that the event was processed.
4. Click Deploy to save the changes.
Create an Amazon EventBridge Rule for DynamoDB 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:
MyDynamoDBEventRule
- Description: Rule to capture DynamoDB table events
- Rule type: Rule with an event pattern
- Event Source: Choose AWS services
- Service Name: Select DynamoDB
- Event Type: All Events (for the simplicity of the lab)
- 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 to the Amazon DynamoDB Console and perform an action that triggers an event, such as Turn on deletion protection.
- A confirmation message will appear; click Confirm.
2. Wait for it to finish.
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 that captures events from a DynamoDB table and triggers a Lambda function. This setup enables you to automate workflows and react to data changes in real-time.
Key Takeaways:
- EventBridge and DynamoDB integration allow for automated, event-driven architecture.
- Using Lambda as a target provides a serverless way to process and act on DynamoDB events.
- CloudWatch logs help you monitor and debug your event-driven workflows.
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!!!