Securing Relational Databases
This lesson explains how securing a relational database combines network isolation, encryption, authentication without stored passwords, and monitoring of database activity.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the network, encryption and monitoring controls for databases.
- Explain how an existing unencrypted database is encrypted.
- Explain how IAM database authentication replaces stored passwords.
Controls
A database is secured in several layers. On the network, it runs in private subnets with no internet route, and its security group allows the database port only from the application tier's security group. Encryption at rest with KMS must be chosen when the database is created; an existing unencrypted database is encrypted by taking a snapshot, copying it with encryption enabled and restoring from the copy.
Encryption in transit is enforced by a parameter that rejects unencrypted connections, rds.force_ssl for PostgreSQL and SQL Server or require_secure_transport for MySQL. For authentication, IAM database authentication lets applications connect with short-lived tokens instead of passwords, and Secrets Manager stores and rotates the master password. For monitoring, database logs go to CloudWatch Logs, and Database Activity Streams send a near real-time record of database activity for auditing. The following table summarises the controls.
| Area | Control |
|---|---|
| Network | Private subnets; security groups allowing only the application tier |
| Encryption at rest | KMS, chosen at creation; to encrypt an existing DB, restore an encrypted snapshot copy |
| Encryption in transit | rds.force_ssl (PostgreSQL, SQL Server) or require_secure_transport (MySQL) |
| Authentication | IAM database authentication (MySQL, PostgreSQL); Secrets Manager for the master password, with rotation |
| Monitoring | Logs to CloudWatch Logs; Database Activity Streams for near real-time activity monitoring |
Database security controls
Encrypting an existing database
Encryption must be chosen when an RDS instance is created; it cannot be enabled on an existing unencrypted instance directly. An encrypted copy is created instead.
-
Snapshot
Take a snapshot of the unencrypted instance.
-
Copy with encryption
Copy the snapshot with encryption enabled, choosing the KMS key.
-
Restore and switch
Restore a new instance from the encrypted snapshot copy and switch the application to it.
Read replicas of an encrypted instance are encrypted with the same key, and an unencrypted instance cannot have an encrypted read replica, so this procedure is the only route to encryption.
Encrypting a database
IAM database authentication
The application generates a short-lived authentication token with its IAM role and uses it as the password over TLS, so no password is stored. The role needs rds-db:connect for the database user.
Securing database access
Database access must be secured at several levels. The following table identifies the control that meets each requirement.
| Requirement | Control |
|---|---|
| No passwords in application code | Secrets Manager with rotation, or IAM database authentication |
| Only the application tier can connect | Database security group allowing the application's security group |
| Not reachable from the internet | Private subnets; Publicly accessible set to No |
| Encrypted at rest | Enabled at creation with KMS (an existing database needs snapshot, encrypted copy, restore) |
| Encrypted in transit | Require TLS with rds.force_ssl or require_secure_transport |
| Thousands of Lambda connections | RDS Proxy |
Encryption at rest is a common trap: it cannot be switched on for a running unencrypted instance, so the architect enables it from the start.
How can an application connect to RDS without a stored database password?
Show answer
With IAM database authentication: the application gets a short-lived token with its IAM role and uses it as the password.
Exam Tips
Remember for the exam
- To connect to MySQL or PostgreSQL with IAM instead of passwords, use IAM database authentication.
- To encrypt an unencrypted RDS instance, take a snapshot, make an encrypted copy and restore it.
- For near real-time monitoring of database activity for compliance, use Database Activity Streams.
- To rotate the master password automatically, store it in Secrets Manager.