Advanced 40 min read Module 6 of 8

AWS Databases

Learn about RDS (relational databases), DynamoDB (NoSQL), and choosing the right database for your needs.

Database Options in AWS

RDS

Relational Databases
MySQL, PostgreSQL, SQL Server

Free Tier Available
DynamoDB

NoSQL Database
Key-Value & Document

Always Free Tier
Aurora

MySQL/PostgreSQL Compatible
5x faster than MySQL

Free Tier Available

RDS - Relational Database Service

RDS is a managed relational database service. AWS handles backups, patching, and replication for you.

Supported Engines: MySQL, PostgreSQL, MariaDB, Oracle, SQL Server
Free Tier: 750 hours/month of db.t2.micro
Backups: Automated daily backups, point-in-time recovery
Read Replicas: Scale read traffic across multiple instances
Connect from your app: Use connection string from RDS console

DynamoDB - NoSQL Database

DynamoDB is a fully managed NoSQL database. It's perfect for high-traffic applications that need low latency.

Speed
Single-digit millisecond latency

Scale
Auto-scales to any size

Free Tier
25 GB free forever

When to use DynamoDB: High-traffic apps, real-time data, flexible schemas, and serverless applications.

How to Choose the Right Database

Use Case Recommended Service Why?
E-commerce, CRM, ERP RDS Complex queries, transactions, relationships
High-traffic web apps DynamoDB Fast, scalable, serverless friendly
Analytics, Data warehousing Redshift Petabyte-scale data analysis
Mobile/Serverless apps DynamoDB Low latency, auto-scaling
Blogs, CMS RDS Simple relational data

Exercise: Create a Database

Task: Create and connect to an RDS database.

  1. Go to RDS and click "Create database"
  2. Choose MySQL or PostgreSQL
  3. Select Free Tier template
  4. Set master username and password
  5. Create the database (takes 5-10 minutes)
  6. Note the endpoint URL
  7. Connect using MySQL Workbench or a Lambda function
  8. Create a table and insert data
Show Solution
# Python Lambda function to connect to RDS
import pymysql
import json

def lambda_handler(event, context):
    connection = pymysql.connect(
        host='your-database-endpoint',
        user='your-username',
        password='your-password',
        database='your-database'
    )
    
    cursor = connection.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS users (id INT, name VARCHAR(100))")
    cursor.execute("INSERT INTO users VALUES (1, 'John Doe')")
    connection.commit()
    
    return {
        'statusCode': 200,
        'body': json.dumps('Database created and data inserted!')
    }
Key Takeaway

Choose RDS for relational data with complex queries. Choose DynamoDB for high-scale, low-latency applications. Both have free tier options to get started!