Skip to content

Rds

Chapter 21: Amazon RDS - Relational Database Service

Section titled “Chapter 21: Amazon RDS - Relational Database Service”

Amazon RDS (Relational Database Service) is a managed service that makes it easier to set up, operate, and scale a relational database in the cloud.

RDS Overview
+------------------------------------------------------------------+
| |
| +------------------------+ |
| | Amazon RDS | |
| +------------------------+ |
| | |
| +---------------------+---------------------+ |
| | | | |
| v v v |
| +----------+ +----------+ +----------+ |
| | Managed | | Multiple | | High | |
| | Database | | Engines | | Availability |
| | | | | | | |
| | - Backup | | - MySQL | | - Multi-AZ |
| | - Patch | | - Postgre| | - Failover |
| | - Scale | | - Oracle | | - Replicas |
| +----------+ | - SQL | +----------+ |
| | - MariaDB| |
| | - Aurora | |
| +----------+ |
| |
+------------------------------------------------------------------+

RDS Database Engines
+------------------------------------------------------------------+
| |
| Engine | Version Support | Use Case |
| ----------------|------------------------|---------------------|
| MySQL | 5.7, 8.0 | Web apps, CMS |
| PostgreSQL | 12-16 | Enterprise apps |
| Oracle | 12c, 19c, 21c | Enterprise |
| SQL Server | 2016-2022 | Windows apps |
| MariaDB | 10.2-10.11 | MySQL compatible |
| Aurora | MySQL/PostgreSQL | Cloud-native |
| |
+------------------------------------------------------------------+

Single-AZ RDS Deployment
+------------------------------------------------------------------+
| |
| Availability Zone A |
| +----------------------------------------------------------+ |
| | | |
| | +------------------+ | |
| | | RDS Primary | | |
| | | Instance | | |
| | | +------------+ | | |
| | | | Database | | | |
| | | | Engine | | | |
| | | +------------+ | | |
| | | | | | |
| | | v | | |
| | | +------------+ | | |
| | | | EBS Volume | | | |
| | | | (Storage) | | | |
| | | +------------+ | | |
| | +------------------+ | |
| | | |
| +----------------------------------------------------------+ |
| |
| Use Case: Development, non-critical workloads |
| |
+------------------------------------------------------------------+
Multi-AZ RDS Deployment
+------------------------------------------------------------------+
| |
| Availability Zone A Availability Zone B |
| +----------------------+ +----------------------+ |
| | | | | |
| | +----------------+ | | +----------------+ | |
| | | RDS Primary | | | | RDS Standby | | |
| | | Instance | | | | Instance | | |
| | | (Active) | | | | (Passive) | | |
| | +----------------+ | | +----------------+ | |
| | | | | ^ | |
| | v | | | | |
| | +----------------+ | | +----------------+ | |
| | | EBS Volume | | | | EBS Volume | | |
| | | (Primary) | | | | (Replica) | | |
| | +----------------+ | | +----------------+ | |
| | | | | ^ | |
| | | | | | | |
| | +-----Synchronous Replication-----+ | |
| | | | | |
| +----------------------+ +----------------------+ |
| |
| Features: |
| - Synchronous replication |
| - Automatic failover |
| - Single DNS endpoint |
| - 99.95% availability SLA |
| |
+------------------------------------------------------------------+
Multi-AZ DB Cluster (One Writer, Two Readers)
+------------------------------------------------------------------+
| |
| Availability Zone A AZ B AZ C |
| +----------------+ +--------+ +--------+ |
| | | | | | | |
| | +----------+ | | +----+ | | +----+ | |
| | | Writer | | | |Reader| | | |Reader| | |
| | | Instance | | | | | | | | | | |
| | +----------+ | | +----+ | | +----+ | |
| | | | | ^ | | ^ | |
| | v | | | | | | | |
| | +----------+ | | | | | | | |
| | | Storage | | | | | | | | |
| | | Volume | | | | | | | | |
| | +----------+ | | | | | | | |
| | | | | | | | | | |
| | +--------+----+----+---+----+----+ | |
| | | | | | |
| | Shared Storage (Cluster Volume) | |
| | | |
| +--------------------------------------------+ |
| |
| Features: |
| - One writer, up to two readers |
| - Shared storage volume |
| - Faster failover (typically 35 seconds) |
| - Read scalability |
| |
+------------------------------------------------------------------+

RDS Read Replicas
+------------------------------------------------------------------+
| |
| Same-Region Read Replica |
| +----------------------------------------------------------+ |
| | | |
| | AZ A AZ B AZ C | |
| | +----------+ +----------+ +----------+ | |
| | | Primary | | Read | | Read | | |
| | | Instance |------>| Replica 1|------>| Replica 2| | |
| | +----------+ +----------+ +----------+ | |
| | | ^ ^ | |
| | | | | | |
| | +----Asynchronous Replication---------+ | |
| | | |
| +----------------------------------------------------------+ |
| |
| Cross-Region Read Replica |
| +----------------------------------------------------------+ |
| | | |
| | US-East-1 EU-West-1 | |
| | +----------+ +----------+ | |
| | | Primary | | Read | | |
| | | Instance |---------->| Replica | | |
| | +----------+ +----------+ | |
| | | ^ | |
| | | | | |
| | +--Cross-Region Replication | |
| | | |
| +----------------------------------------------------------+ |
| |
| Features: |
| - Asynchronous replication |
| - Read scaling |
| - Can be promoted to primary |
| - Up to 5 read replicas per primary |
| - Cross-region for DR |
| |
+------------------------------------------------------------------+

RDS Storage Types
+------------------------------------------------------------------+
| |
| General Purpose SSD (gp2/gp3) |
| +----------------------------------------------------------+ |
| | | |
| | Features: | |
| | - Cost-effective SSD | |
| | - 3 IOPS per GB (gp2) | |
| | - Up to 16,000 IOPS (gp3) | |
| | - Burst capability | |
| | | |
| | Use Case: | |
| | - Development | |
| | - Medium workloads | |
| | | |
| +----------------------------------------------------------+ |
| |
| Provisioned IOPS SSD (io1/io2) |
| +----------------------------------------------------------+ |
| | | |
| | Features: | |
| | - Highest performance | |
| | - Up to 80,000 IOPS | |
| | - Predictable latency | |
| | | |
| | Use Case: | |
| | - Production databases | |
| | - I/O-intensive workloads | |
| | | |
| +----------------------------------------------------------+ |
| |
| Magnetic (Standard) - Legacy |
| +----------------------------------------------------------+ |
| | | |
| | Features: | |
| | - Lowest cost | |
| | - Limited performance | |
| | | |
| | Use Case: | |
| | - Small databases | |
| | - Rarely accessed data | |
| | | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
RDS Storage Autoscaling
+------------------------------------------------------------------+
| |
| How it works: |
| +----------------------------------------------------------+ |
| | | |
| | 1. Enable storage autoscaling | |
| | 2. Set maximum storage threshold | |
| | 3. RDS monitors storage usage | |
| | 4. Automatically increases when threshold reached | |
| | | |
| | Triggers: | |
| | - Free storage < 10% of allocated | |
| | - Or within 5 minutes of reaching threshold | |
| | | |
| | Scaling: | |
| | - Increases in 5 GB increments | |
| | - No downtime | |
| | | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

RDS Network Security
+------------------------------------------------------------------+
| |
| VPC Configuration |
| +----------------------------------------------------------+ |
| | | |
| | DB Subnet Group: | |
| | +----------------------------------------------------+ | |
| | | | | |
| | | Subnet 1 (AZ-a) Subnet 2 (AZ-b) Subnet 3 (AZ-c)| | |
| | | +----------+ +----------+ +----------+ | | |
| | | | Private | | Private | | Private | | | |
| | | | Subnet | | Subnet | | Subnet | | | |
| | | +----------+ +----------+ +----------+ | | |
| | | | | |
| | +----------------------------------------------------+ | |
| | | |
| | Security Group: | |
| | +----------------------------------------------------+ | |
| | | Inbound: | | |
| | | - TCP 3306 (MySQL) from app SG | | |
| | | - TCP 5432 (PostgreSQL) from app SG | | |
| | | | | |
| | | Outbound: | | |
| | | - None required | | |
| | +----------------------------------------------------+ | |
| | | |
| +----------------------------------------------------------+ |
| |
| Public vs Private: |
| +----------------------------------------------------------+ |
| | Public Access: | |
| | - Not recommended for production | |
| | - Use for development only | |
| | | |
| | Private Only: | |
| | - Recommended for production | |
| | - Access via VPC, VPN, or Direct Connect | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
RDS Encryption
+------------------------------------------------------------------+
| |
| Encryption at Rest |
| +----------------------------------------------------------+ |
| | | |
| | Features: | |
| | - AES-256 encryption | |
| | - AWS KMS integration | |
| | - Encrypts storage, logs, backups | |
| | - Must enable at creation | |
| | | |
| | Limitations: | |
| | - Cannot encrypt existing unencrypted DB | |
| | - Must create new encrypted DB and migrate | |
| | | |
| +----------------------------------------------------------+ |
| |
| Encryption in Transit |
| +----------------------------------------------------------+ |
| | | |
| | Features: | |
| | - SSL/TLS connections | |
| | - Enforce via parameter group | |
| | - rds.force_ssl = 1 | |
| | | |
| | Connection: | |
| | mysql -h mydb.xxxx.rds.amazonaws.com \ | |
| | --ssl-ca=rds-ca-2019-root.pem \ | |
| | --ssl-mode=REQUIRED | |
| | | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

RDS Automated Backups
+------------------------------------------------------------------+
| |
| Features: |
| +----------------------------------------------------------+ |
| | | |
| | - Enabled by default | |
| | - Retention: 1-35 days | |
| | - Point-in-time recovery (PITR) | |
| | - Transaction logs backed up every 5 minutes | |
| | | |
| | Backup Window: | |
| | - Default: Random 30-minute window | |
| | - Can customize | |
| | - Brief I/O suspension (Single-AZ) | |
| | | |
| +----------------------------------------------------------+ |
| |
| Point-in-Time Recovery |
| +----------------------------------------------------------+ |
| | | |
| | Timeline: | |
| | |----|----|----|----|----|----|----|----| | |
| | ^ ^ ^ ^ ^ ^ ^ ^ ^ | |
| | | | | | | | | | | | |
| | Full Log Log Log Log Log Log Log Now | |
| | Backup | |
| | | |
| | Recovery: | |
| | - Select any point in retention period | |
| | - Creates new DB instance | |
| | | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
RDS Manual Snapshots
+------------------------------------------------------------------+
| |
| Features: |
| +----------------------------------------------------------+ |
| | | |
| | - Manually triggered | |
| | - Retained until explicitly deleted | |
| | - No expiration | |
| | - Can share with other accounts | |
| | - Can copy to other regions | |
| | | |
| | Use Cases: | |
| | - Pre-change backups | |
| | - Long-term retention | |
| | - Cross-region disaster recovery | |
| | | |
| +----------------------------------------------------------+ |
| |
| Snapshot Copy |
| +----------------------------------------------------------+ |
| | | |
| | Source Region Destination Region | |
| | +----------------+ +----------------+ | |
| | | Snapshot | | Snapshot Copy | | |
| | | (Manual) |---->| (Encrypted) | | |
| | +----------------+ +----------------+ | |
| | | |
| | Features: | |
| | - Cross-region copy | |
| | - Can re-encrypt with different key | |
| | - For disaster recovery | |
| | | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

# ============================================================
# DB Subnet Group
# ============================================================
resource "aws_db_subnet_group" "main" {
name = "main-subnet-group"
subnet_ids = var.private_subnet_ids
tags = {
Name = "main-db-subnet-group"
}
}
# ============================================================
# RDS Parameter Group
# ============================================================
resource "aws_db_parameter_group" "main" {
name = "main-params"
family = "mysql8.0"
parameter {
name = "character_set_server"
value = "utf8mb4"
}
parameter {
name = "character_set_client"
value = "utf8mb4"
}
parameter {
name = "max_connections"
value = "500"
}
parameter {
name = "slow_query_log"
value = "1"
}
tags = {
Name = "main-parameter-group"
}
}
# ============================================================
# RDS Instance (Multi-AZ)
# ============================================================
resource "aws_db_instance" "main" {
identifier = "main-db"
# Engine
engine = "mysql"
engine_version = "8.0"
instance_class = "db.m6g.large"
# Storage
allocated_storage = 100
max_allocated_storage = 500 # Autoscaling
storage_type = "gp3"
storage_encrypted = true
kms_key_id = aws_kms_key.rds.arn
# Database
db_name = "appdb"
username = "admin"
password = var.db_password
# Network
db_subnet_group_name = aws_db_subnet_group.main.name
vpc_security_group_ids = [aws_security_group.rds.id]
publicly_accessible = false
# High Availability
multi_az = true
availability_zone = "us-east-1a" # Primary AZ
# Parameter Group
parameter_group_name = aws_db_parameter_group.main.name
# Backup
backup_retention_period = 30
backup_window = "03:00-04:00"
skip_final_snapshot = false
final_snapshot_identifier = "main-db-final"
# Maintenance
maintenance_window = "Mon:04:00-Mon:05:00"
# Performance Insights
performance_insights_enabled = true
performance_insights_kms_key_id = aws_kms_key.rds.arn
# Deletion protection
deletion_protection = true
# Monitoring
enabled_cloudwatch_logs_exports = ["error", "general", "slow_query"]
monitoring_interval = 60
monitoring_role_arn = aws_iam_role.rds_monitoring.arn
tags = {
Name = "main-db"
}
}
# ============================================================
# Read Replica
# ============================================================
resource "aws_db_instance" "read_replica" {
identifier = "main-db-replica"
# Source
replicate_source_db = aws_db_instance.main.arn
# Instance class (can differ from primary)
instance_class = "db.m6g.large"
# Network
vpc_security_group_ids = [aws_security_group.rds.id]
# Storage (inherited from source)
storage_encrypted = true
kms_key_id = aws_kms_key.rds.arn
# Performance Insights
performance_insights_enabled = true
tags = {
Name = "main-db-replica"
}
}
# Cross-Region Read Replica
resource "aws_db_instance" "cross_region_replica" {
provider = aws.dr_region
identifier = "main-db-dr-replica"
# Source (from snapshot or replication)
replicate_source_db = aws_db_instance.main.arn
instance_class = "db.m6g.large"
vpc_security_group_ids = [aws_security_group.rds_dr.id]
tags = {
Name = "main-db-dr-replica"
}
}
# ============================================================
# Security Group
# ============================================================
resource "aws_security_group" "rds" {
name = "rds-sg"
description = "Security group for RDS"
vpc_id = var.vpc_id
ingress {
description = "MySQL from application"
from_port = 3306
to_port = 3306
protocol = "tcp"
security_groups = [aws_security_group.app.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "rds-sg"
}
}
# ============================================================
# IAM Role for Enhanced Monitoring
# ============================================================
resource "aws_iam_role" "rds_monitoring" {
name = "rds-monitoring-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "monitoring.rds.amazonaws.com"
}
}
]
})
}
resource "aws_iam_role_policy_attachment" "rds_monitoring" {
role = aws_iam_role.rds_monitoring.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
}
# ============================================================
# Multi-AZ DB Cluster (MySQL)
# ============================================================
resource "aws_rds_cluster" "main" {
cluster_identifier = "main-cluster"
engine = "aurora-mysql"
engine_version = "8.0.mysql_aurora.3.02.0"
database_name = "appdb"
master_username = "admin"
master_password = var.db_password
# Network
db_subnet_group_name = aws_db_subnet_group.main.name
vpc_security_group_ids = [aws_security_group.rds.id]
# Encryption
storage_encrypted = true
kms_key_id = aws_kms_key.rds.arn
# Backup
backup_retention_period = 30
preferred_backup_window = "03:00-04:00"
# Cluster settings
deletion_protection = true
skip_final_snapshot = false
final_snapshot_identifier = "main-cluster-final"
tags = {
Name = "main-cluster"
}
}
# Cluster Instances
resource "aws_rds_cluster_instance" "writer" {
identifier = "main-cluster-writer"
cluster_identifier = aws_rds_cluster.main.id
instance_class = "db.r6g.large"
engine = aws_rds_cluster.main.engine
engine_version = aws_rds_cluster.main.engine_version
performance_insights_enabled = true
tags = {
Name = "main-cluster-writer"
}
}
resource "aws_rds_cluster_instance" "reader" {
count = 2
identifier = "main-cluster-reader-${count.index + 1}"
cluster_identifier = aws_rds_cluster.main.id
instance_class = "db.r6g.large"
engine = aws_rds_cluster.main.engine
engine_version = aws_rds_cluster.main.engine_version
performance_insights_enabled = true
tags = {
Name = "main-cluster-reader-${count.index + 1}"
}
}

Exam Tip

  1. Multi-AZ: Synchronous replication, automatic failover, single endpoint
  2. Read Replicas: Asynchronous, up to 5 per DB, read scaling
  3. Cross-Region: Read replica for DR, can be promoted
  4. Storage Types: gp3 (general), io1/io2 (high IOPS)
  5. Storage Autoscaling: Automatic increase, no downtime
  6. Encryption: At rest (KMS), in transit (SSL), must enable at creation
  7. Backups: Automated (1-35 days), Manual (no expiration)
  8. PITR: Point-in-time recovery, 5-minute granularity
  9. Parameter Groups: Engine configuration, custom settings
  10. Maintenance Window: Patching, minor version upgrades

Chapter 22: Amazon Aurora - Cloud-Native Database


Last Updated: February 2026