AWS Logo
Menu

Implementing RDS Event Notification with Terraform

In this post, I will introduce a Terraform module that sends notifications from RDS clusters and instances to a Slack channel

Published Jan 4, 2025

Problem

Today, many organizations use RDS for their applications because of its excellent scaling capabilities and operational features. RDS provides multiple deployment options, giving users the flexibility to choose what best fits their use cases
For example, at my organization, we leverage RDS Aurora clusters that mix provisioned instances and serverless-v2 instances. We chose this architecture because it provides an optimal balance between high availability, performance, scaling, and cost
For example, our application is write-heavy, so we want to keep a provisioned instance as the primary node while using serverless instances as secondary nodes in the cluster. At the same performance level, provisioned instances are more cost-effective than serverless ones
Aurora cluster in expected situation
Aurora cluster in expected situation
If there are issues with the primary node, the cluster will perform auto-failover, and a serverless instance will become the primary node. In most cases, that serverless instance will run at maximum capacity. During normal operations when the primary node is the provisioned instance, serverless instances run at minimum capacity
Aurora cluster in not-expecting situation
Aurora cluster in not-expecting situation
Since serverless instances are expensive, we want to minimize their time as the primary node. Therefore, we need a way to detect when this happens. RDS event notification provides this capability

Solution

We use Amazon RDS event notifications to receive prompt alerts whenever cluster-related events occur. Here's the high-level architecture:
High-level architecture
High-level architecture
  • First, we subscribe to Amazon RDS event notification. This requires setting up a SNS topic to receive the event messages
  • Next, a Lambda function is used to process the event messages from the SNS topic and forward them to Slack using a webhook
  • Finally, the event message is delivered to Slack, notifying cluster operators about what’s happening in their clusters
This is what you’ll see in the Slack channel when a failover occurs:
Cluster failover started
Cluster failover started
DB instance shutdown
DB instance shutdown
Cluster failover completed
Cluster failover completed
The solution is simple yet highly effective
At our organization, we’ve decided that every cluster must implement this solution. To streamline this process, we developed a Terraform module rds-event-notification. This ensures that whenever a cluster is provisioned, event notifications are automatically set up as well
Here’s how to use the module:

Conclusion

RDS event notification provides a simple but effective way to monitor and respond to important database events. Key points:
  • Enables real-time monitoring of RDS clusters and instances through SNS notifications
  • Integrates smoothly with Slack for immediate team alerts about failovers and other critical events
  • Implemented as a reusable Terraform module for consistent deployment across multiple clusters
  • Particularly valuable for hybrid setups using both provisioned and serverless instances where cost optimization is important
This solution helps teams maintain high availability while keeping costs under control by enabling quick responses to database events
 

Comments