
Project 9: Connect RDS (Database) to EC2
Launch a private RDS database and connect it to your EC2 app over a secure connection.
Part 9 of 12 — 12 Hands-On AWS Projects
Introduction
So far, our projects have been stateless — the pages show the same content every time, and nothing is saved. Real applications remember things: users, orders, movie lists. Today we will give our app a memory. We will launch an Amazon RDS database in a private subnet and connect it to an EC2 instance, so the web page can read and write real data.
This is the moment a static demo becomes a real application.
What you will build
- An RDS MySQL database running in a private subnet — no public internet access
- An EC2 instance running a small app that talks to the database
- A security group rule that lets only the EC2 instance reach the database
- A working connection test, proved with a real query from the app server
- The habit of keeping databases private and reaching them only from inside the VPC
Architecture
Visitors open the web page on the EC2 instance in a public subnet. The app on that instance opens a connection to the RDS database in a private subnet — the database has no public IP address, so the internet cannot reach it at all. A security group on the database allows MySQL traffic on port 3306 only from the EC2 instance's security group.

Prerequisites
- An AWS account (free tier covers RDS for 12 months)
- One EC2 instance in the default VPC, with a security group that allows HTTP (port 80) and SSH (port 22)
- A MySQL client on the EC2 instance — install one with your package manager if needed
- A web browser
Steps
1. Create the RDS database
In the RDS console, create a database. Choose MySQL as the engine and the Free tier template. Give the database a name like netflix-db, set a master username and a strong password, and store the password safely. Under Connectivity, choose the default VPC, and set Public access to No — the database will live in a private subnet. Create the database and wait until its status shows Available. Note down the endpoint address — it looks like netflix-db.abc123.ap-south-1.rds.amazonaws.com.
2. Allow the EC2 instance to reach the database
Open the security group attached to your RDS database. Add an inbound rule: type MySQL/Aurora, port 3306, and as the source choose the security group of your EC2 instance — not an IP address, the security group itself. This means only resources carrying that security group can connect. Nothing else on the internet can even knock on the database's door.
3. Connect from the EC2 instance
SSH into your EC2 instance and test the connection with the MySQL client:
mysql -h <rds-endpoint> -u admin -pReplace <rds-endpoint> with the endpoint address from step 1, and type the master password when asked. If the prompt changes to mysql>, you are connected. Try a quick check:
SHOW DATABASES;You should see the databases on your RDS instance listed back.
4. Create a table and store real data
While connected, create a small table and add a row — this is your app's data layer working:
CREATE DATABASE netflix_app;USE netflix_app;CREATE TABLE movies (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(100), genre VARCHAR(50));INSERT INTO movies (title, genre) VALUES ('Inception', 'Sci-Fi'), ('3 Idiots', 'Comedy');SELECT * FROM movies;You should see both rows returned. Your web app can now read this table and show real movies from the database instead of hard-coded text.
What you learned
- RDS is a managed database — AWS runs the engine, patching and backups for you
- A database belongs in a private subnet, with no public IP address
- Security groups can reference each other, so only your app server can reach the database
- The endpoint address is how your app finds the database — never hard-code an IP
- A real query returning real rows is the proof the connection works
Cleanup
An RDS instance costs money even when idle, so delete it when you finish learning. In the RDS console, delete the database — choose not to create a final snapshot for a practice setup — then terminate the EC2 instance. Double-check that nothing is left running before you close the console.
Try next
In Project 10, we will connect two separate networks. You will build two VPCs and join them with VPC peering, so a resource in one VPC can talk privately to a resource in the other.
About Cloud Messenger — Learn • Build • Visualize • Troubleshoot • Grow | cloudmessenger.in | linkedin.com/in/cloud-messenger | github.com/Cloud-Messenger | #buildwithCloudMessenger
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article