"RDS & EC2 Sitting in a Tree: A Love Story in the AWS Cloud"

Shreyash MyakalShreyash Myakal
3 min read

As a cloud and DevOps enthusiast, you're bound to encounter Amazon RDS—a managed relational database service by AWS. If you're new to it, this blog will help you understand what RDS is, how it works, and how to practically use it with EC2.


📘 What is Amazon RDS?

Amazon RDS (Relational Database Service) is a managed database service that allows you to run relational databases like MySQL, PostgreSQL, Oracle, and more, without needing to manage the underlying infrastructure.

It handles:

  • Backups

  • Software patching

  • Monitoring

  • Scaling

  • Replication


⚙️ How RDS Works

When you create an RDS instance, AWS provisions a dedicated DB instance on the cloud. You don’t have to manually install or configure the database on a server. Instead, you manage it via the AWS Console or CLI. It gives you an endpoint, which applications (or EC2) can use to connect and perform database operations.


💡 Why Use RDS?

  • Easy to Set Up: No need to install and configure databases manually.

  • 🔒 Secure: Integrated with AWS IAM, VPC, and security groups.

  • 📈 Scalable: You can vertically scale compute/storage with a few clicks.

  • 📦 Backups and Recovery: Automated backups and snapshots.

  • 🧠 Monitoring: Integrated with CloudWatch for performance tracking.


🛠️ Practical Guide: Connect EC2 with RDS

Let’s walk through 2 methods of connecting an EC2 instance to an RDS database.


🔹 Method 1: Create RDS First, Then Connect via EC2

✅ Step 1: Create RDS MySQL Database

  1. Go to AWS Console > RDS > Databases > Create database.

  2. Choose Standard create.

  3. Engine type: Select MySQL.

  4. Template: Choose Free tier (if eligible).

  5. Set credentials (e.g., username: admin, password: your choice).

  6. In "Connectivity", DO NOT select EC2 option.

  7. Leave the rest as default and click Create database.

✅ Step 2: Launch EC2 & Install MySQL

  1. Go to EC2 > Instances > Launch instance.

  2. Choose Amazon Linux 2 AMI, instance type t2.micro, and allow SSH access.

  3. Connect via SSH using EC2's public IP.

  4. Install MySQL:

     bashCopyEditsudo yum update -y
     sudo yum install mysql -y
    

    ❌ Don't start MySQL locally. We’ll connect to RDS.

✅ Step 3: Connect EC2 to RDS

  1. Go to RDS > Databases > [Your DB] > Connectivity & security.

  2. Copy the endpoint (e.g., mydb.xyz123.us-east-1.rds.amazonaws.com).

  3. In EC2 terminal, connect using:

     bashCopyEditsudo mysql -u admin -p -h <RDS-endpoint>
    

If it fails to connect, read Method 2 to understand and fix the issue.


🔹 Method 2: EC2 Can't Access RDS? Fix Security Group

✅ Step 1: Identify the Problem

By default, RDS is locked down by its security group, which may not allow connections from your EC2 instance.

✅ Step 2: Fix the Inbound Rule

  1. In RDS > Connectivity, check the Security group attached to RDS (likely named default).

  2. Go to EC2 > Network & Security > Security Groups, and search for the default security group.

  3. Click Inbound Rules > Edit Inbound Rules.

  4. Add a new rule:

    • Type: Custom TCP

    • Port: 3306

    • Source: Select EC2’s security group

  5. Save the rule.

✅ Step 3: Connect Again

Now try again from EC2:

bashCopyEditsudo mysql -u admin -p -h <RDS-endpoint>

🎉 You’re now connected to RDS from EC2!


✅ Conclusion

Amazon RDS simplifies database management while giving you high availability, security, and scalability. By understanding how to properly set up security groups and connectivity, you can easily connect EC2 with RDS and begin building cloud-based applications.

0
Subscribe to my newsletter

Read articles from Shreyash Myakal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shreyash Myakal
Shreyash Myakal

I’m currently learning Linux, AWS, DevOps, MySQL, and related technologies, aiming to become a Cloud Engineer. Passionate about cloud infrastructure and automation, I’m excited to apply these skills in real-world projects.