AWS Tutorial menu

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

bash
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

sql
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;
Output
+--------+---------+
| 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_id and order_date

Working with DynamoDB

Create a table:

Example

bash
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_REQUEST

Add an item:

Example

bash
aws dynamodb put-item --table-name Orders \
  --item '{"order_id": {"S": "1001"}, "customer": {"S": "Ali"}, "total_rs": {"N": "2500"}}'

Read it back:

Example

bash
aws dynamodb get-item --table-name Orders --key '{"order_id": {"S": "1001"}}'
Output
{
    "Item": {
        "order_id": {"S": "1001"},
        "customer": {"S": "Ali"},
        "total_rs": {"N": "2500"}
    }
}

The letters S and N mean String and Number.

RDS or DynamoDB?

QuestionRDSDynamoDB
Data modelTables with fixed columnsFlexible items
Query languageSQL, with joinsKey-based reads, no joins
ScalingChoose a bigger instance sizeScales automatically
Good forSchools, accounts, shops with related dataShopping carts, sessions, games, IoT data
BillingPer hour of the instancePer 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?

Learn AWS with a teacher

Join the CIT (Computer Information Technology) course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp