Amazon RDS
Amazon Relational Database Service (RDS) runs relational databases such as PostgreSQL and MySQL for you. AWS handles installation, patching, backups and failover, so you get a production-grade SQL database without becoming a full-time database administrator.
What "managed" means
If you installed PostgreSQL on an EC2 instance yourself, you would have to install it, configure it, patch it, set up backups, test restores, replicate it to another server and handle failures at 3 a.m. With RDS:
| AWS handles | You still handle |
|---|---|
| Hardware, OS and database engine installation | Schema design, queries and indexes |
| Engine patching in a maintenance window you choose | Choosing the instance size and storage |
| Automated backups and point-in-time restore | Network access (security groups, subnets) |
| Automatic failover with Multi-AZ | Database users, passwords and permissions |
| Monitoring metrics in CloudWatch | Acting on performance problems |
You cannot SSH into the database server; you connect with normal database clients and tools.
Engines
RDS supports PostgreSQL, MySQL, MariaDB, Oracle, Microsoft SQL Server and IBM Db2. AWS also offers Amazon Aurora, its own cloud-native engine compatible with MySQL and PostgreSQL, which separates compute from a distributed storage layer kept in multiple AZs, and has a serverless option that scales capacity with demand. For Django projects, PostgreSQL on RDS or Aurora PostgreSQL is a common choice (see Django deployment).
High availability and scaling
- Multi-AZ deployment: RDS keeps a synchronous standby copy in a different AZ. If the primary fails, or during some maintenance, RDS fails over automatically, usually within a minute or two, and the database's endpoint name stays the same. The standby in the classic setup does not serve traffic. (A newer Multi-AZ DB cluster option has two readable standbys.)
- Read replicas: asynchronous copies that serve read-only queries, useful when many users read the same data, like students checking results. They can be in other Regions and can be promoted to standalone databases.
- Scaling up: change to a larger instance class, or enable storage autoscaling so disk space grows automatically.
A school results portal writes marks a few times a term but is read by thousands of students on results day. A sensible design: a Multi-AZ primary for writes and safety, plus one or two read replicas, with the app sending read-only queries to the replicas.
Backups
- Automated backups take a daily snapshot and keep transaction logs, so you can restore to any second within the retention period (up to 35 days).
- Manual snapshots are kept until you delete them; take one before a risky change.
- A restore always creates a new database instance; you then point your app at it.
Creating a database
A secure setup keeps the database in private subnets, not publicly accessible, with a security group that allows the database port only from the app's security group. This CLI example assumes a DB subnet group and security group already exist:
aws rds create-db-instance \
--db-instance-identifier school-portal-db \
--engine postgres \
--db-instance-class db.t4g.micro \
--allocated-storage 20 \
--master-username portaladmin \
--manage-master-user-password \
--no-publicly-accessible \
--db-subnet-group-name school-private-subnets \
--vpc-security-group-ids sg-0db00000000000001 \
--backup-retention-period 7 \
--storage-encrypted
aws rds wait db-instance-available --db-instance-identifier school-portal-db
aws rds describe-db-instances --db-instance-identifier school-portal-db \
--query "DBInstances[0].Endpoint.Address" --output text
--manage-master-user-password makes RDS generate the admin password and store it in Secrets Manager, rotating it for you, so the password never appears in your terminal history.
Connecting from Python
From an app running inside the VPC (for example on EC2 or Lambda with VPC access), read the secret and connect with psycopg 3. The secret ARN and host are placeholders:
import json
import boto3
import psycopg
SECRET_ARN = "arn:aws:secretsmanager:eu-west-1:123456789012:secret:rds!db-example"
HOST = "school-portal-db.abcdefghijkl.eu-west-1.rds.amazonaws.com"
secret = json.loads(
boto3.client("secretsmanager").get_secret_value(SecretId=SECRET_ARN)["SecretString"]
)
with psycopg.connect(host=HOST, dbname="postgres", user=secret["username"],
password=secret["password"], sslmode="require") as conn:
with conn.cursor() as cur:
cur.execute("SELECT version()")
print(cur.fetchone()[0])
The exact version string depends on the engine version you chose.
Do not make a database "publicly accessible" with a security group open to 0.0.0.0/0 just to connect from your laptop. Bots constantly scan for open database ports. Connect through a bastion with Session Manager port forwarding, or from inside the VPC.
Costs and clean-up
- You pay per hour for the instance class (Multi-AZ roughly doubles it), per GB-month of storage, for backup storage beyond your database size, and for data transfer. See the RDS pricing page.
- You can stop an RDS instance to save compute charges, but RDS starts it again automatically after seven days. For practice databases, delete them instead.
- When deleting, choose whether to keep a final snapshot. Snapshots also cost money, so delete ones you do not need.
Key points
- RDS runs relational databases with patching, backups and failover handled by AWS.
- Engines include PostgreSQL, MySQL, MariaDB, Oracle, SQL Server, Db2, plus Aurora.
- Multi-AZ gives automatic failover; read replicas scale reads.
- Keep databases private, store credentials in Secrets Manager and allow access only from app security groups.
- Stopped RDS instances restart after seven days; delete practice databases.
Tip: use the ← and → keys to move between pages.
