Overview
This module creates:- RDS PostgreSQL 15.4 instance
- Database subnet group in private subnets
- Custom parameter group for PostgreSQL configuration
- Enhanced monitoring with 60-second granularity
- Automated backups with configurable retention
- Performance Insights for query analysis
Resources Created
RDS Instance
- Resource:
aws_db_instance.main - Engine: PostgreSQL 15.4
- Storage: gp3 SSD with auto-scaling
- Encryption: Enabled with KMS
- Accessibility: Private subnets only
Database Subnet Group
- Resource:
aws_db_subnet_group.main - Groups private subnets from networking module
- RDS can fail over to any subnet in the group
Parameter Group
- Resource:
aws_db_parameter_group.postgres - Family: postgres15
- Custom Parameters:
log_min_duration_statement = 1000- Log queries slower than 1 secondlog_connections = 1- Log all connection attemptstimezone = UTC- Consistent with Kubernetes cluster
IAM Role for Monitoring
- Resource:
aws_iam_role.rds_monitoring - Allows RDS to send enhanced monitoring metrics to CloudWatch
- Attached policy:
AmazonRDSEnhancedMonitoringRole
Variables
Project name for resource naming and tagging
Deployment environment:
dev, staging, or prodAffects:- Deletion protection (enabled in prod)
- Final snapshot (created in prod, skipped in dev)
IDs of private subnets where RDS will be deployed. Use
private_subnet_ids from networking module.Security group ID allowing access from EKS to RDS on port 5432. Use
rds_security_group_id from networking module.RDS instance class determining CPU and memory.Recommendations by environment:
db.t3.micro: Dev (1 vCPU, 1GB RAM) - ~$15/monthdb.t3.small: Staging (2 vCPUs, 2GB RAM) - ~$30/monthdb.r6g.large: Production (2 vCPUs, 16GB RAM) - ~$120/month
Initial storage allocation in GB. Minimum 20 GB for gp3.
Maximum storage for auto-scaling. Set to
0 to disable auto-scaling.RDS automatically increases storage when:- Free space falls below 10%
- Low space persists for 5 minutes
- 6 hours have passed since last modification
Name of the initial database created on the instance
Master username for PostgreSQL. Avoid using
postgres or admin.Master password for PostgreSQL.Security:
- Do not hardcode in Terraform files
- Use AWS Secrets Manager or environment variables
- Minimum 8 characters
- Should contain uppercase, lowercase, numbers, and symbols
Enable Multi-AZ deployment for high availability.
true: Synchronous replica in different AZ, automatic failover in 1-2 minutesfalse: Single instance (recommended for dev to reduce costs)
trueNumber of days to retain automated backups (1-35).Recommendations:
- Dev: 1-3 days
- Staging: 7 days
- Production: 14-30 days
Outputs
RDS instance identifier
Connection endpoint in format
hostname:port (e.g., db.abc.us-east-1.rds.amazonaws.com:5432)Sensitive: Contains internal hostnameHostname only, without port. Use for application configuration.
Database port (default: 5432)
Name of the initial database
Master username
Name of the DB subnet group
ARN of the RDS instance for IAM policies and monitoring
Usage Example
Connection Configuration
Application Connection String
PostgreSQL URL format:Using AWS Secrets Manager
Recommended for production:Backup and Recovery
Automated Backups
- Daily automated backups during maintenance window
- Default backup window: 03:00-04:00 UTC
- Retention: Configurable via
backup_retention_days - Point-in-time recovery to any second within retention period
Manual Snapshots
Create manual snapshot before major changes:Restore from Snapshot
Performance Monitoring
Enhanced Monitoring
Provides OS-level metrics every 60 seconds:- CPU utilization (detailed)
- Memory usage
- Disk I/O
- Network throughput
- Process list
Performance Insights
Enabled by default with 7-day retention (free tier). Features:- Real-time query performance analysis
- Top SQL statements by execution time
- Database load visualization
- Wait event analysis
CloudWatch Alarms
Create alarms for critical metrics:Security Best Practices
Network Isolation
- RDS deployed in private subnets only
- No public accessibility (
publicly_accessible = false) - Security group restricts access to EKS cluster only
Encryption
- At rest: All data encrypted using KMS
- In transit: Enforce SSL connections in application:
Deletion Protection
Enabled automatically in production:- Prevents accidental deletion
- Must be manually disabled before destroying instance
Password Management
Never hardcode passwords:Maintenance and Updates
Maintenance Window
Default: Monday 04:00-05:00 UTC Scheduled maintenance includes:- OS patches
- Database engine patches
- Infrastructure updates
Minor Version Upgrades
Automatically applied during maintenance window:- Example: PostgreSQL 15.4 → 15.5
- Disabled with
auto_minor_version_upgrade = false
Major Version Upgrades
Must be performed manually:Cost Optimization
Development Environments
Production Optimization
- Use Reserved Instances for 1-3 year commitment (up to 60% savings)
- Monitor storage growth and right-size
max_allocated_storage - Use Performance Insights to identify and optimize slow queries
- Consider Aurora Serverless for variable workloads
Troubleshooting
Cannot Connect from EKS
- Verify security group allows port 5432 from EKS security group
- Check pods are in same VPC as RDS
- Test connectivity from pod:
Storage Full
If auto-scaling is disabled or max limit reached:High CPU Usage
- Check Performance Insights for slow queries
- Review query execution plans
- Add database indexes
- Consider scaling up instance class