Connecting to RDS via EC2

Shivam Shrivastava
AWS Tip
Published in
4 min readAug 7, 2022

--

Always thought it was a pain to configure all the network rules to connect to an RDS instance and to be honest; as it turns out there aren’t many rules to configure. I was just too lazy to learn!

Hopefully I can summarize this process and do it briefly for the lazy peeps like me out there:

Step 1: Create a database on the AWS Relational Database Service(RDS):
Open the AWS Management console -> Click on “Create Database”
Here we have multiple options/flavors to choose from -> Choose “MySQL”-> Select “Free Tier” under “Templates”

Under “Settings”, please provide the “DB Instance identifier” and the “Master username” and “Master password” you will use to connect to the instance:

Under connectivity form select the VPC within which our RDS instance will reside -> here you can pick your default VPC. After this scroll to the end of the page and click on create database and wait for the instance to be created.

Step 2: Now let us create the EC2 instance we will be using to connect to RDS:

Navigate to the EC2 service and click on “Launch instance” -> Select free tier eligible options OS as “Amazon Linux” and Instance Type as “t2.micro”

Create and download (create option automatically downloads the new key to local) or choose a “Key Pair” which will be used as an authentication key/credentials to connect with the instance.

Under the option “Allow SSH traffic from” -> select your IP as shown below in the screenshot. This way the instance is secured and can only be connected via your IP. Post this click on “Launch instance”.

Step 3: Connecting to the newly created instance. Move to the “Instances” dashboard and select the instance we created and click on “Connect” option.

Select “SSH client” tab as shown below and follow the instructions to connect to the EC2 instance. For windows users you need to install putty for establishing a connection or any other SSH application.

Once the connection is established we would need to install the MySql on it-> run the following command to do the same “sudo yum install mysql”.

To connect to RDS using EC2 we would need to use the port 3306. For this copy the IPV4.

Open the RDS console and navigate to “Connectivity & security tab”, click on your “VPC security groups”

This will navigate you to the security group console; at the bottom of the page you can find the “Inbound rules” tab -> click on “Edit inbound rules”:

Here, add a new rule with “Port Range” as “3306” and under IP text box add the private IP you copied followed by “/32” and save the rule.

Let us finally connect to RDS from our EC2 instance -> Navigate to RDS instance -> copy the endpoint from “Connectivity and security” tab and run the following command on your terminal “mysql -u admin -p -h [endpoint]”.

All set to execute SQL commands on your RDS instance. Go wild!

--

--

Data Engineer at Fractal. Enthusiastic about learning, jotting those learnings down and sharing with the world.