Creating a rule that triggers on an Amazon Aurora event
Using Amazon EventBridge, you can automate AWS services and respond to system events such as application availability issues or resource changes.
Tutorial: Log DB instance state changes using Amazon EventBridge
In this tutorial, you create an AWS Lambda function that logs the state changes for an instance. You then create a rule that runs the function whenever there is a state change of an existing RDS DB instance. The tutorial assumes that you have a small running test instance that you can shut down temporarily.
Important
Don't perform this tutorial on a running production DB instance.
Step 1: Create an AWS Lambda function
Create a Lambda function to log the state change events. You specify this function when you create your rule.
To create a Lambda function
Open the AWS Lambda console at https://console.aws.amazon.com/lambda/
. -
If you're new to Lambda, you see a welcome page. Choose Get Started Now. Otherwise, choose Create function.
-
Choose Author from scratch.
-
On the Create function page, do the following:
-
Enter a name and description for the Lambda function. For example, name the function
RDSInstanceStateChange
. -
In Runtime, select Node.js 16x.
-
For Architecture, choose x86_64.
-
For Execution role, do either of the following:
-
Choose Create a new role with basic Lambda permissions.
-
For Existing role, choose Use an existing role. Choose the role that you want to use.
-
-
Choose Create function.
-
-
On the RDSInstanceStateChange page, do the following:
-
In Code source, select index.js.
-
In the index.js pane, delete the existing code.
-
Enter the following code:
console.log('Loading function'); exports.handler = async (event, context) => { console.log('Received event:', JSON.stringify(event)); };
-
Choose Deploy.
-
Step 2: Create a rule
Create a rule to run your Lambda function whenever you launch an Amazon RDS instance.
To create the EventBridge rule
Open the Amazon EventBridge console at https://console.aws.amazon.com/events/
. -
In the navigation pane, choose Rules.
-
Choose Create rule.
-
Enter a name and description for the rule. For example, enter
RDSInstanceStateChangeRule
. -
Choose Rule with an event pattern, and then choose Next.
-
For Event source, choose AWS events or EventBridge partner events.
-
Scroll down to the Event pattern section.
-
For Event source, choose AWS services.
-
For AWS service, choose Relational Database Service (RDS).
-
For Event type, choose RDS DB Instance Event.
-
Leave the default event pattern. Then choose Next.
-
For Target types, choose AWS service.
-
For Select a target, choose Lambda function.
-
For Function, choose the Lambda function that you created. Then choose Next.
-
In Configure tags, choose Next.
-
Review the steps in your rule. Then choose Create rule.
Step 3: Test the rule
To test your rule, shut down an RDS DB instance. After waiting a few minutes for the instance to shut down, verify that your Lambda function was invoked.
To test your rule by stopping a DB instance
Open the Amazon RDS console at https://console.aws.amazon.com/rds/
. -
Stop an RDS DB instance.
Open the Amazon EventBridge console at https://console.aws.amazon.com/events/
. -
In the navigation pane, choose Rules, choose the name of the rule that you created.
-
In Rule details, choose Monitoring.
You are redirected to the Amazon CloudWatch console. If you are not redirected, click View the metrics in CloudWatch.
-
In All metrics, choose the name of the rule that you created.
The graph should indicate that the rule was invoked.
-
In the navigation pane, choose Log groups.
-
Choose the name of the log group for your Lambda function (/aws/lambda/
function-name
). -
Choose the name of the log stream to view the data provided by the function for the instance that you launched. You should see a received event similar to the following:
{ "version": "0", "id": "12a345b6-78c9-01d2-34e5-123f4ghi5j6k", "detail-type": "RDS DB Instance Event", "source": "aws.rds", "account": "111111111111", "time": "2021-03-19T19:34:09Z", "region": "us-east-1", "resources": [ "arn:aws:rds:us-east-1:111111111111:db:testdb" ], "detail": { "EventCategories": [ "notification" ], "SourceType": "DB_INSTANCE", "SourceArn": "arn:aws:rds:us-east-1:111111111111:db:testdb", "Date": "2021-03-19T19:34:09.293Z", "Message": "DB instance stopped", "SourceIdentifier": "testdb", "EventID": "RDS-EVENT-0087" } }
For more examples of RDS events in JSON format, see Overview of events for Aurora.
-
(Optional) When you're finished, you can open the Amazon RDS console and start the instance that you stopped.