Skip to main content

Command Palette

Search for a command to run...

🔧 Hands-on: Connecting an EC2 Instance to Amazon RDS MySQL (Including Read Replica)

Published
3 min read
🔧 Hands-on: Connecting an EC2 Instance to Amazon RDS MySQL (Including Read Replica)

📌 Objective

  • Understand AWS RDS fundamentals through hands-on practice

  • Create and connect a MySQL RDS instance securely

  • Implement backups using snapshots

  • Configure and test Read Replicas

  • Restore a database from a snapshot


🧱 Prerequisites

Before starting, ensure the following are ready:

  • AWS Account

  • Basic knowledge of EC2, VPC, and Security Groups

  • Default or custom VPC with:

    • At least two private subnets (required for RDS subnet group)

Step 1️⃣ Create DB Subnet Group (Pre-requisite)

RDS requires a DB Subnet Group with subnets in at least two AZs.

Steps:

  1. Go to RDS → Subnet groups

  2. Create a subnet group

  3. Select your VPC

  4. Add private subnets from 2 AZs


Step 2️⃣ Create RDS MySQL DB Instance

Configuration Used:

  • Engine: MySQL

  • Version: Free-tier eligible

  • Template: Free Tier

  • DB instance identifier: Give name

  • Credentials: Username & password

  • Connectivity:

    • VPC: Same as EC2

    • Public access: ❌ No

    • DB Subnet Group: Created earlier

  • Availability:

    • Single AZ (initially)


Step 3️⃣ Launch EC2 Instance

Steps:

  1. Launch Amazon Linux EC2

  2. Place it in a public subnet

  3. Attach a Security Group allowing:

    • SSH (22) from your IP
  4. Use the same VPC as RDS


Step 4️⃣ Configure Security Groups

EC2 Security Group

  • Allow SSH (22)

RDS Security Group

  • Allow MySQL (3306)

  • Source: EC2 Security Group (not IP-based)

This ensures secure internal communication.


Step 5️⃣ Install MySQL Client on EC2

After connecting to EC2 via SSH, I installed the MySQL client using the following commands:

# Switch to root

sudo su -

# Install dependencies

yum install wget -y

# Download MySQL repo

wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

md5sum mysql57-community-release-el7-9.noarch.rpm

rpm -ivh mysql57-community-release-el7-9.noarch.rpm

# Install MySQL

yum install mysql-server -y

sudo yum install mysql -y --nogpgcheck

sudo yum install mysql-connector-java.noarch -y --nogpgcheck


Step 6️⃣ Connect EC2 to RDS MySQL

Used the RDS endpoint to connect:

mysql -h rds-endpoint-name -P 3306 -u username -p

After login, I executed basic SQL commands to verify connectivity:

  • Create database

  • Create table

  • Insert records

  • Select data


Step 7️⃣ Create Snapshot (Backup)

Snapshots are manual backups of the DB instance.

Steps:

  1. Go to RDS → Databases

  2. Select DB instance

  3. Actions → Take snapshot

  4. Provide snapshot name


Step 8️⃣ Create Read Replica

Read Replicas help scale read-heavy workloads.

Steps:

  1. Select primary DB

  2. Actions → Create read replica

  3. Same region

  4. Separate DB identifier

Key Points:

  • Uses asynchronous replication

  • Has a separate endpoint

  • Used only for READ operations


Step 9️⃣ Connect to Read Replica

To connect to the read replica, I used only the read replica endpoint:

mysql -h read-replica-endpoint-name -P 3306 -u username -p

Verified:

  • SELECT queries work

  • INSERT/UPDATE not allowed


Step 🔟 Create New DB Instance from Snapshot

Steps:

  1. Go to RDS → Snapshots

  2. Select snapshot

  3. Actions → Restore snapshot

  4. Configure DB identifier & settings

This creates a new independent DB instance.


✅ Key Learnings

  • RDS is a fully managed database service

  • Subnet groups are mandatory for RDS

  • EC2-to-RDS access must be via Security Groups

  • Snapshots enable point-in-time recovery

  • Read Replicas scale reads without affecting writes


📎 Conclusion

This hands-on lab helped me gain real-world exposure to AWS RDS MySQL architecture, security, backups, and scaling strategies. Practicing these steps strengthened my understanding of how production-grade databases are designed on AWS.


Follow for more:

Linkedin: https://www.linkedin.com/in/devops-samarjeet/

#AWS #CloudComputing #DevOps #AWSArchitecture #EC2 #VPC #ALB #NATGateway #AWSBeginner #CloudLearning #InfrastructureAsCode #AWSHandsOn #TechBlog

More from this blog

Sam's blog

56 posts