AWS Databases: RDS and DynamoDB
AWS offers managed databases, so you can store data without installing and looking after database software yourself.
Why a Managed Database?
If you install MySQL on your own EC2 server, you must handle updates, backups, disk space and failures yourself. A managed database does most of this for you:
- Automatic backups and software patches
- Easy scaling to a bigger size
- Copies in another Availability Zone for high availability
AWS has many database services. The two most popular for beginners are RDS and DynamoDB.
Amazon RDS (Relational Database Service)
RDS runs relational (SQL) databases. Data is stored in tables with rows and columns, and you use SQL to read and write it.
Supported engines include:
- MySQL and MariaDB
- PostgreSQL
- Microsoft SQL Server and Oracle
- Amazon Aurora (AWS's own MySQL and PostgreSQL compatible engine)
Creating an RDS Database
In the console: RDS then Create database, choose MySQL, the Free tier template, a database name, a master user name and password.
With the CLI:
Example
aws rds create-db-instance \
--db-instance-identifier school-db \
--engine mysql \
--db-instance-class db.t3.micro \
--allocated-storage 20 \
--master-username admin \
--master-user-password 'Str0ng-Pass-2026'After a few minutes, RDS gives you an endpoint (a host name). Your app connects to it like any MySQL server:
Example
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
city VARCHAR(30)
);
INSERT INTO students (name, city) VALUES ('Ayesha', 'Karachi'), ('Bilal', 'Lahore');
SELECT name, city FROM students;+--------+---------+ | name | city | +--------+---------+ | Ayesha | Karachi | | Bilal | Lahore | +--------+---------+
Keep RDS databases private: do not make them publicly accessible. Let only your app's security group connect on the database port (3306 for MySQL).
Multi-AZ and Read Replicas
- Multi-AZ: AWS keeps a standby copy in another Availability Zone. If the main one fails, it switches automatically.
- Read replicas: extra copies that handle read queries, so a busy website stays fast.
Amazon DynamoDB
DynamoDB is a NoSQL key-value and document database. It has no fixed columns: each item can have different attributes. It is serverless, so there is no server size to choose, and it is very fast even with millions of users.
Every table needs a primary key. It can be:
- A partition key only, such as
order_id - A partition key + sort key, such as
customer_idandorder_date
Working with DynamoDB
Create a table:
Example
aws dynamodb create-table \
--table-name Orders \
--attribute-definitions AttributeName=order_id,AttributeType=S \
--key-schema AttributeName=order_id,KeyType=HASH \
--billing-mode PAY_PER_REQUESTAdd an item:
Example
aws dynamodb put-item --table-name Orders \
--item '{"order_id": {"S": "1001"}, "customer": {"S": "Ali"}, "total_rs": {"N": "2500"}}'Read it back:
Example
aws dynamodb get-item --table-name Orders --key '{"order_id": {"S": "1001"}}'{
"Item": {
"order_id": {"S": "1001"},
"customer": {"S": "Ali"},
"total_rs": {"N": "2500"}
}
}The letters S and N mean String and Number.
RDS or DynamoDB?
| Question | RDS | DynamoDB |
|---|---|---|
| Data model | Tables with fixed columns | Flexible items |
| Query language | SQL, with joins | Key-based reads, no joins |
| Scaling | Choose a bigger instance size | Scales automatically |
| Good for | Schools, accounts, shops with related data | Shopping carts, sessions, games, IoT data |
| Billing | Per hour of the instance | Per request or reserved capacity |
If you already know SQL and your data has many relations, start with RDS. If you need huge scale with simple lookups by key, choose DynamoDB.
Exercise
Exercise
Which AWS database service is a serverless NoSQL key-value database?
