Creating a rule that triggers on an Amazon RDS event - Amazon Relational Database Service

Creating a rule that triggers on an Amazon RDS event

Using Amazon CloudWatch Events and Amazon EventBridge, you can automate AWS services and respond to system events such as application availability issues or resource changes.

Creating rules to send Amazon RDS events to CloudWatch Events

You can write simple rules to indicate which Amazon RDSAmazon Aurora events interest you and which automated actions to take when an event matches a rule. You can set a variety of targets, such as an AWS Lambda function or an Amazon SNS topic, which receive events in JSON format. For example, you can configure Amazon RDS to send events to CloudWatch Events or Amazon EventBridge whenever a DB instance is created or deleted. For more information, see the Amazon CloudWatch Events User Guide and the Amazon EventBridge User Guide.

Use the following steps to create a CloudWatch Events rule that triggers on an event emitted by an AWS service.

To create a rule that triggers on an event:

  1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.

  2. Under Events in the navigation pane, choose Rules.

  3. Choose Create rule.

  4. For Event Source, do the following:

    1. Choose Event Pattern.

    2. For Service Name, choose Relational Database Service (RDS).

    3. For Event Type, choose the type of Amazon RDS resource that triggers the event. For example, if a DB instance triggers the event, choose RDS DB Instance Event.

  5. For Targets, choose Add Target and choose the AWS service that is to act when an event of the selected type is detected.

  6. In the other fields in this section, enter information specific to this target type, if any is needed.

  7. For many target types, CloudWatch Events needs permissions to send events to the target. In these cases, CloudWatch Events can create the IAM role needed for your event to run:

    • To create an IAM role automatically, choose Create a new role for this specific resource.

    • To use an IAM role that you created before, choose Use existing role.

  8. Optionally, repeat steps 5-7 to add another target for this rule.

  9. Choose Configure details. For Rule definition, type a name and description for the rule.

    The rule name must be unique within this Region.

  10. Choose Create rule.

For more information, see Creating a CloudWatch Events Rule That Triggers on an Event in the Amazon CloudWatch User Guide.

Tutorial: log the state of an Amazon RDS instance using EventBridge

You can create an AWS Lambda function that logs the state changes for an Amazon RDS instance. You can choose to create a rule that runs the function whenever there is a state transition or a transition to one or more states that are of interest.

In this tutorial, you log any 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 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

  1. Open the AWS Lambda console at https://console.aws.amazon.com/lambda/.

  2. If you're new to Lambda, you see a welcome page. Choose Get Started Now. Otherwise, choose Create function.

  3. Choose Author from scratch.

  4. On the Create function page, do the following:

    1. Enter a name and description for the Lambda function. For example, name the function RDSInstanceStateChange.

    2. In Runtime, select Node.js 14x.

    3. In Execution role, choose Create a new role with basic Lambda permissions. For Existing role, select your basic execution role. Otherwise, create a basic execution role.

    4. Choose Create function.

  5. On the RDSInstanceStateChange page, do the following:

    1. In Code source, select index.js.

    2. Right-click index.js, and choose Open.

    3. In the index.js pane, delete the existing code.

    4. Enter the following code:

      console.log('Loading function'); exports.handler = async (event, context) => { console.log('Received event:', JSON.stringify(event)); };
    5. 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

  1. Open the Amazon EventBridge console at https://console.aws.amazon.com/events/.

  2. In the navigation pane, choose Rules.

  3. Choose Create rule.

  4. Enter a name and description for the rule. For example, enter RDSInstanceStateChangeRule.

  5. For Define pattern, do the following:

    1. Choose Event pattern.

    2. Choose Pre-defined pattern by service.

    3. For Service provider, choose AWS.

    4. For Service Name, choose Relational Database Service (RDS).

    5. For Event type, choose RDS DB Instance Event.

  6. For Select event bus, choose AWS default event bus. When an AWS service in your account emits an event, it always goes to your account’s default event bus.

  7. For Target, choose Lambda function.

  8. For Function, select the Lambda function that you created.

  9. Choose Create.

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

  1. Open the Amazon RDS console at https://console.aws.amazon.com/rds/.

  2. Stop an RDS DB instance.

  3. Open the Amazon EventBridge console at https://console.aws.amazon.com/events/.

  4. In the navigation pane, choose Rules, choose the name of the rule that you created.

  5. In Rule details, choose Metrics for the rule.

    You are redirected to the Amazon CloudWatch console.

  6. In All metrics, choose the name of the rule that you created.

    The graph should indicate that the rule was invoked.

  7. In the navigation pane, choose Log groups.

  8. Choose the name of the log group for your Lambda function (/aws/lambda/function-name).

  9. 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" } }
  10. (Optional) When you're finished, you can open the Amazon RDS console and start the instance that you stopped.