Day 44: Relational Database Service in AWS
Amazon Relational Database Service (Amazon RDS) simplifies setting up, operating, and scaling databases in the cloud. It automates tasks like hardware provisioning, database setup, patching, and backups. In this blog, we’ll walk through how to create and connect a Free Tier RDS instance of MySQL with an EC2 instance.
Objectives
Set up a MySQL database using RDS in AWS.
Connect the database with an EC2 instance via a MySQL client.
Task 01: Create a Free Tier RDS Instance of MySQL
Steps:
Login to AWS Console:
- Navigate to the AWS Management Console.
Open RDS Service:
- Search for RDS in the AWS console search bar and click on it.
Create Database:
Click on the "Create Database" button.
Select Engine Type: Choose MySQL.
Choose Edition: Select the Free Tier option.
Database Settings:
Set a DB Identifier (e.g.,
mydb
).Provide a Master Username (e.g.,
admin
).Set a Master Password and confirm it.
Instance Configuration:
For Free Tier, choose db.t2.micro instance type.
Storage: Select the default 20 GB storage.
Network & Security:
Place the RDS in a VPC.
Ensure the Public Access option is enabled to connect externally.
Create or use an existing security group that allows MySQL traffic (port 3306).
Launch the Database:
- Click Create Database and wait for the instance to become Available.
Task 02: Create an EC2 Instance
Steps:
Open EC2 Service:
- Navigate to the EC2 dashboard from the AWS Console.
Launch Instance:
Click Launch Instances.
Select an Amazon Linux 2 AMI or Ubuntu as the instance type.
Instance Settings:
Choose the t2.micro instance for Free Tier eligibility.
Ensure the EC2 instance is in the same VPC and Availability Zone as the RDS instance.
Configure Security Group:
Allow inbound SSH (port 22) for your IP.
Add a rule to allow outbound MySQL (port 3306) for communication with RDS.
Launch Instance:
Create or select a key pair for secure login.
Launch the instance.
Task 03: Create an IAM Role with RDS Access
Steps:
Open IAM Console:
- Navigate to the IAM service in AWS.
Create a Role:
Choose AWS Service as the trusted entity type.
Select EC2 as the service.
Attach Policy:
- Attach the AmazonRDSFullAccess policy to grant RDS permissions.
Name and Create:
- Name the role (e.g.,
RDSAccessRole
) and create it.
- Name the role (e.g.,
Task 04: Assign the Role to EC2 Instance
Steps:
Go to EC2 Dashboard:
- Select the EC2 instance you created earlier.
Attach IAM Role:
Click Actions > Security > Modify IAM Role.
Choose the role you created (e.g.,
RDSAccessRole
).
Task 05: Connect EC2 Instance to RDS Instance
Steps:
SSH into EC2 Instance:
Open a terminal and connect to your EC2 instance:
ssh -i "your-key.pem" ec2-user@your-ec2-public-ip
Install MySQL Client:
For Amazon Linux:
sudo apt-get install mysql -y
For Ubuntu:
sudo apt update sudo apt install mysql-client -y
Connect to RDS:
Use the credentials from the RDS console to connect:
mysql -h <RDS-endpoint> -u <MasterUsername> -p
Replace
<RDS-endpoint>
with the endpoint URL of your RDS instance, and provide the master password when prompted.
Verify Connection:
Run MySQL commands to ensure the connection is successful:
SHOW DATABASES; CREATE DATABASE testdb;
Key Notes
Security Groups: Ensure both EC2 and RDS security groups allow traffic on port 3306.
IAM Role: The role simplifies access management by eliminating the need to store credentials on the EC2 instance.
Free Tier Eligibility: Both RDS and EC2 must meet Free Tier requirements to avoid charges.
Conclusion
By completing this task, you’ve learned to set up a managed database with Amazon RDS and securely connect it to an EC2 instance. This foundational skill is essential for cloud-based applications, enabling robust and scalable database solutions.
Subscribe to my newsletter
Read articles from Dhruv Moradiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by