Relational Database Service in AWS

Table of contents

Introduction:

Amazon Relational Database Service (RDS) is a fully managed relational database offered by Amazon Web Services (AWS). It simplifies the setup, operation, and scaling of relational databases in the cloud. RDS helps developers focus on their applications by handling routine database management tasks like backups, patching, and replication.


Key Features of AWS RDS

  1. Support for Multiple Database Engines
    RDS supports popular relational database engines, including:

    • Amazon Aurora

    • MySQL

    • PostgreSQL

    • MariaDB

    • Oracle Database

    • Microsoft SQL Server

  2. Automated Backups
    AWS RDS provides automated backups, point-in-time recovery, and manual snapshots for data protection. This ensures data is safe and can be restored in case of accidental loss or failure.

  3. High Availability
    RDS offers high availability with the Multi-AZ (Availability Zone) deployment option. This creates a synchronous standby replica in a different Availability Zone to ensure minimal downtime.

  4. Read Replicas
    To improve performance and scalability, RDS offers read replicas. These replicas handle read-heavy workloads and help distribute traffic without affecting the primary database's performance.

  5. Automatic Scaling
    RDS allows vertical scaling, meaning you can increase or decrease instance size to match your workload demands. Amazon Aurora (a fully managed RDS database engine) offers serverless scaling options.

  6. Security
    RDS integrates with AWS Identity and Access Management (IAM) for access control and Amazon Virtual Private Cloud (VPC) for network isolation. It also supports encryption for data at rest and in transit using AWS Key Management Service (KMS).

  7. Monitoring and Performance Insights
    AWS provides detailed performance monitoring with CloudWatch metrics and RDS Performance Insights, helping you analyze and optimize database performance.


Use Cases

  • Web and Mobile Applications: RDS is suitable for managing relational databases in scalable, secure, and highly available environments.

  • E-Commerce Applications: The service ensures low-latency data access and real-time inventory management.

  • Data Warehousing: RDS can be used for reporting, aggregation, and structured data analytics.


Pricing Model

RDS offers pay-as-you-go pricing, meaning you pay only for the database instance hours, storage, and I/O operations consumed. AWS also offers Reserved Instances and Savings Plans to optimize costs.

Task-01

  • Create a Free tier RDS instance of MySQL

  • Create an EC2 instance

  • Create an IAM role with RDS access

  • Assign the role to EC2 so that your EC2 Instance can connect with RDS

  • Once the RDS instance is up and running, get the credentials and connect your EC2 instance using a MySQL client.

Follow the step-by-step process below to complete the task:


1. Create a Free Tier RDS MySQL Instance

  1. Login to AWS Console: Navigate to the AWS Management Console.

  2. Open RDS Service:

    • Choose "Create Database."

    • Select MySQL as the engine.

    • Choose Free Tier in the "Templates" section.

  3. Configure the Database:

    • Database name: mydb (example).

    • Master username: admin.

    • Set a password or enable auto-generated passwords.

  4. Instance Specifications:

    • Choose db.t2.micro (eligible for Free Tier).

    • Storage: 20 GB (General Purpose SSD).

  5. VPC and Security Group:

    • Ensure that the database is in a default or accessible VPC.

    • Create or use an existing security group that allows inbound MySQL traffic (Port 3306).

  6. Database Options:

    • Disable "Public Access" unless explicitly needed.

    • Enable automatic backups if desired.

  7. Create Database: Click "Create Database" and wait for the instance to be provisioned.


2. Create an EC2 Instance

  1. Open the EC2 Service:

    • Click "Launch Instance."
  2. Instance Details:

    • AMI: Choose Amazon Linux 2 or Ubuntu (Free Tier eligible).

    • Instance Type: t2.micro.

    • Key Pair: Create or select a key pair.

  3. Security Group Configuration:

    • Allow inbound SSH (Port 22) access from your IP.

    • Ensure the security group allows outbound traffic to Port 3306 (MySQL).

  4. Launch the Instance: Wait for the instance to be ready.


3. Create an IAM Role with RDS Access

  1. Open the IAM Service:

    • Go to "Roles" and click "Create Role."
  2. Trusted Entity:

    • Select "AWS Service" and choose EC2.
  3. Attach Policy:

    • Add the policy AmazonRDSFullAccess (or a custom policy with specific permissions).
  4. Create Role: Give the role a name EC2RDSRole and complete the creation.


4. Assign the IAM Role to the EC2 Instance

  1. Go to the EC2 Dashboard:

    • Select the EC2 instance you created.

    • Click "Actions" → "Security" → "Modify IAM Role."

    • Attach the EC2RDSRole role to the instance.


5. Connect EC2 to RDS Using MySQL Client

  1. Retrieve RDS Credentials:

  2. Connect to EC2 via SSH:

    • Use your key pair to SSH into the EC2 instance:

        ssh -i "your-key.pem" ec2-user@your-ec2-public-ip
      
  3. Install MySQL Client (if not installed):
    For Amazon Linux:

     sudo yum install mysql -y
    

    For Ubuntu:

     sudo apt update
     sudo apt install mysql-client -y
    
  4. Connect to the RDS MySQL Database:
    Use the MySQL client to connect to the database:

     mysql -h your-rds-endpoint -P 3306 -u admin -p
    
  5. Enter the Password: Enter the RDS password to establish the connection.


Verification: Once connected, you can run a simple query to verify:

SHOW DATABASES;

This completes the setup and connection of an EC2 instance with an RDS MySQL instance on AWS.

Deploy WordPress website on AWS

Task-02

  • As WordPress requires a MySQL database to store its data, create an RDS.

To configure this WordPress site, you will create the following resources in AWS:

  • An Amazon EC2 instance to install and host the WordPress application.

  • An Amazon RDS for MySQL database to store your WordPress data.

  • Set up the server and post your new WordPress app.


Here’s a step-by-step guide to deploying a WordPress site on AWS using an EC2 instance and RDS for MySQL.

1. Create an RDS MySQL Database

  1. Go to RDS Console:

    • Navigate to the AWS RDS service in the AWS Console.

    • Click "Create Database".

  2. Choose Database Engine:

    • Select MySQL.

    • Choose "Free Tier" as the deployment option.

  3. Database Settings:

    • DB Name: wordpress.

    • Master Username: admin.

    • Password: Create a strong password or auto-generate one.

  4. Instance Type:

    • Choose db.t2.micro (Free Tier eligible).

    • Storage: 20 GB (General Purpose SSD).

  5. Connectivity:

    • Ensure that the RDS instance is in the same VPC as your EC2 instance.

    • Enable Public Access (for demo purposes).

    • Ensure that the security group allows inbound traffic on Port 3306 (MySQL).

  6. Create the Database:

    • Click "Create Database" and wait for the RDS instance to become available.

2. Launch an EC2 Instance for WordPress

  1. Go to EC2 Console:

    • Click "Launch Instance".
  2. Instance Configuration:

    • Choose Amazon Linux 2 or Ubuntu (Free Tier eligible).

    • Instance type: t2.micro.

    • Create or select an existing key pair.

  3. Security Group Configuration:

    • Allow inbound SSH (Port 22) for admin access from your IP.

    • Allow inbound HTTP (Port 80) for web traffic.

    • Ensure outbound access to Port 3306 to allow connection to RDS.

  4. Launch the Instance:

    • Click "Launch" and wait for the instance to be ready.

3. Install LAMP Stack on the EC2 Instance

  1. Connect to the EC2 instance via SSH:

     ssh -i "your-key.pem" ec2-user@your-ec2-public-ip
    
  2. Update Packages:

     sudo yum update -y
    
  3. Install Apache:

     sudo yum install httpd -y
     sudo systemctl start httpd
     sudo systemctl enable httpd
    
  4. Install PHP and MySQL Client:

     sudo amazon-linux-extras enable php8.0
     sudo yum install php php-mysqlnd -y
    

4. Install WordPress

  1. Download WordPress:

     wget https://wordpress.org/latest.tar.gz
     tar -xvzf latest.tar.gz
     sudo cp -r wordpress/* /var/www/html/
    
  2. Set Correct Permissions:

     sudo chown -R apache:apache /var/www/html
     sudo chmod -R 755 /var/www/html
    
  3. Restart Apache:

     sudo systemctl restart httpd
    

5. Connect WordPress to RDS MySQL

  1. Get RDS Endpoint:

    • Go to the RDS Console and copy the endpoint of your MySQL database.
  2. Edit WordPress Configuration:

    • Rename the sample configuration file:

        sudo mv /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
      
    • Open the file for editing:

        sudo nano /var/www/html/wp-config.php
      
    • Update the following lines with your RDS database details:

        define('DB_NAME', 'wordpress');
        define('DB_USER', 'admin');
        define('DB_PASSWORD', 'your-rds-password');
        define('DB_HOST', 'your-rds-endpoint:3306');
      
    • Save and close the file.


6. Access the WordPress Installation

  1. Open a web browser and navigate to http://your-ec2-public-ip.

  2. You should see the WordPress installation page.

  3. Follow the steps to:

    • Set a site title.

    • Create an admin username and password.

    • Complete the installation.


7. Optional: Secure Your WordPress Installation (Let’s Encrypt)

To enable HTTPS:

  1. Install Certbot:

     sudo amazon-linux-extras enable epel
     sudo yum install certbot python3-certbot-apache -y
    
  2. Generate SSL Certificate:

     sudo certbot --apache
    
  3. Follow Prompts to set up SSL for your site.