🚀 Introduction
In this blog, we will explore how to set up and connect to an AWS RDS database while running a Docker container on an EC2 instance. We will walk through the process of creating an RDS instance, configuring it for secure access, and pulling a Docker image on an EC2 instance to interact with the database. By the end of this guide, you’ll have a practical understanding of how to integrate AWS RDS with a containerized application on EC2.
🔶 Whats is AWS RDS
AWS RDS as a managed database service that simplifies database setup, operation and scaling.
Purpose: handling administrative tasks like backups, patching, monitoring and scaling.
🔶 Aurora offers:
Up to 5x the throughput of MySQL Community Edition & 3x of PostGres
Up to 128 TB of autoscaling SSD storage
Six-way replication across three Availability Zones
Up to 15 read replicas with replica lag under 10-ms
Automatic monitoring with failover
🔶 Benefits of Using RDS:
High availability and fault tolerance.
Vertical and Horizontal Scaling
Automated backups and recovery.
Read replicas for improved read performance
Multi AZ setup for DR (Disaster Recovery)
Cost-effectiveness.
Go in RDS, Create RDS and follow the exact step as shown in figure below.
🔶 AWS RDS Instance Configuration Options Meaning and When to use
Serverless v2 – Best for unpredictable workloads, auto-scales based on demand, cost-efficient for sporadic usage but expensive if used continuously. Ideal for APIs, SaaS, and analytics workloads.
Memory Optimized (db.r5, db.r6g, db.x2g, etc.) – High RAM-to-vCPU ratio, optimized for read-heavy and large in-memory workloads, ensures low latency but is costly. Best for eCommerce, enterprise DBs, and real-time analytics.
Burstable Classes (db.t3, db.t4g, etc.) – Low-cost with occasional CPU bursts, suitable for small workloads but not for sustained high performance. Ideal for blogs, test environments, internal tools, and small web apps.
Remember to give public access because we are accessing it from outside.
Now launch EC2 Instance. Under Security Group Allow HTTP traffic, from the internet option. As shown in below picture.
After Successfully launching EC2 Instance. Now install docker in that instance or follow the steps as shown in below pictures.
sudo apt install docker.io sudo service docker start sudo usermod -aG docker start sudo docker pull philippaul/node-mysql-app:02
sudo systemctl start docker sudo systemctl status docker
After that go back where u made database and select that database that you have made and below that there will be endpoint, copy that and follow the given picture as it is.
Paste that Endpoint after DB_HOST”<endpoint url>”
sudo docker images
sudo docker run --rm -p 80:3000 -e DB_HOST"<endpoint url>" -e DB_USER="admin" -e DB_PASSWORD="<Password>" -d philippaul/node-mysql-app:02
OR
sudo docker stop $(sudo docker ps -q) sudo docker run --rm -p 80:3000 \ -e DB_HOST="endpoint url" \ -e DB_USER="admin" \ -e DB_PASSWORD="password" \ -d philippaul/node-mysql-app:02
After that will run successfully and show something like as shown in below picture.
Run this command on your EC2 instance to check if the RDS instance is accessible.
nc -zv <endpoint url> 3306
If you get a connection refused or timeout, then:
Ensure that RDS is in the same VPC as your EC2 instance.
Check RDS security groups:
Add an inbound rule to allow MySQL traffic (
3306
) or selectanywhere IPv4
from your EC2 instance's security group.Example rule: Type: MySQL/Aurora | Protocol: TCP | Port Range: 3306 | Source: Custom <EC2 security group ID> or click
anywhere IPv4
Ensure Public Accessibility is set to
Yes
if your EC2 instance is in another network.
After fixing security group issues, test the MySQL connection using.
mysql -h <endpoint url> -u admin -p
Restart Your Container & Check Logs.
sudo docker stop $(sudo docker ps -q) sudo docker run --rm -p 80:3000 \ -e DB_HOST="endpoint url" \ -e DB_USER="admin" \ -e DB_PASSWORD="password" \ -d philippaul/node-mysql-app:02 sudo docker logs -f $(sudo docker ps -q)
sudo docker ps sudo docker logs -f <container name>
After successfully Database is ready. Now copy public ipv4 oc EC2 and paste it on browser.
After that, add username (just for a example).
Type the below command it will rebuild your image and after that it will ask for password so type password that you have created while creating RDS (Database)
sudo docker run -it --rm mysql:8.0 mysql -h <endpoint url> -u admin -p
Now u are enter in database.
Type
show database;
it will show what your website contains in database. (like email, phone no etc)Type
show tables;
which contains contacts or details.Type
select * from contacts;
it will show data of user. (Like we have add our username as shown in the above picture (point 12))Now I have deleted xyz@gmail.com username it will also update in database instantly. (Follow below picture to understand it in better way)
Now go back to RDS, select database that you have created, under that there is option logs and event in event you can see what you have done at which timing, and under logs you can see your errors
Suppose your database got deleted you can restore it from snapshot, also copy and share your snapshot as it is in another region.
🚀Conclusion
In this blog, we have seen how to set up an AWS RDS database and connect it with a Docker container running on an EC2 instance. We walked through creating the RDS instance, configuring access, pulling a Docker image, and ensuring seamless integration between the two. This setup provides a scalable and efficient way to manage databases while leveraging the flexibility of containerized applications. By following these steps, you can deploy similar architectures for your own projects, optimizing performance and reliability in cloud environments.
Thanks for reading to the end; I hope you gained some knowledge.❤️🙌