Connecting EC2 with RDS MySQL Instance Using AWS
Introduction to Amazon RDS
Amazon Relational Database Service (Amazon RDS) is a managed service that makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while automating time-consuming administrative tasks such as hardware provisioning, database setup, patching, and backups. Amazon RDS supports several database engines, including MySQL, PostgreSQL, MariaDB, Oracle, and Microsoft SQL Server.
Task-01: Creating a Free Tier RDS Instance of MySQL
Step 1: Create an RDS Instance
Open the AWS Management Console:
Go to the AWS Management Console at AWS Console.
Sign in to your AWS account.
Navigate to the RDS Dashboard:
- In the AWS Management Console, click on "Services" and select "RDS" under the "Database" section.
Create Database:
Click on the "Create database" button.
Choose the "Standard Create" option.
Select "MySQL" as the engine type.
For the "Edition," choose the "Free tier" option.
Configure Database Settings:
Set a DB instance identifier (e.g.,
mydbinstance
).Set a Master username (e.g.,
admin
).Set a Master password and confirm it.
Configure Instance Details:
Choose the instance type (e.g.,
db.t2.micro
).Set the allocated storage (e.g., 20 GB).
Connectivity:
Set up a Virtual Private Cloud (VPC) or select an existing one.
Enable Public Accessibility if you need to access the database from outside the VPC.
Additional Configuration:
Choose default settings or customize them based on your needs.
Click "Create database."
Step 2: Create an EC2 Instance
Navigate to the EC2 Dashboard:
- In the AWS Management Console, click on "Services" and select "EC2" under the "Compute" section.
Launch Instance:
- Click on the "Launch Instance" button.
Choose an Amazon Machine Image (AMI):
- Select the "Amazon Linux 2 AMI (HVM), SSD Volume Type."
Choose an Instance Type:
- Select the "t2.micro" instance type, which is eligible for the AWS free tier.
Configure Instance Details:
- Click "Next: Configure Instance Details." You can keep the default settings here and click "Next: Add Storage."
Add Storage:
- The default storage size is typically sufficient. Click "Next: Add Tags."
Add Tags:
- (Optional) Add tags to your instance to help manage and identify it later. Click "Next: Configure Security Group."
Configure Security Group:
Create a new security group or select an existing one.
Add a rule to allow SSH traffic from your IP address:
Type: SSH
Protocol: TCP
Port Range: 22
Source: My IP
Review and Launch:
Click "Review and Launch."
Click "Launch."
Select an existing key pair or create a new one. Make sure to download the key pair (.pem file) as you will need it to connect to your instance.
Step 3: Create an IAM Role with RDS Access
Navigate to the IAM Dashboard:
- In the AWS Management Console, click on "Services" and select "IAM" under "Security, Identity, & Compliance."
Create Role:
Click on "Roles" in the left-hand menu.
Click on "Create role."
Choose "AWS Service" and select "EC2."
Click "Next: Permissions."
Attach Policies:
Search for "AmazonRDSFullAccess" and select it.
Click "Next: Tags" and then "Next: Review."
Enter a role name (e.g.,
EC2RDSAccessRole
) and click "Create role."
Attach Role to EC2 Instance:
Navigate to the EC2 Dashboard.
Select your EC2 instance.
Click on "Actions," then "Security," and select "Modify IAM Role."
Choose the role you created (
EC2RDSAccessRole
) and click "Update IAM Role."
Step 4: Connect EC2 Instance to RDS Using MySQL Client
Connect to Your EC2 Instance:
Open your terminal (Mac/Linux) or Git Bash (Windows).
Connect using SSH:
ssh -i "your-key-pair.pem" ec2-user@your-ec2-public-dns
Replace
your-key-pair.pem
with the name of your key pair file andyour-ec2-public-dns
with the Public DNS (IPv4) of your instance, which can be found on the instance details page in the AWS console.
Install MySQL Client on EC2:
sudo yum update -y sudo yum install mysql -y
Connect to the RDS Instance:
Get the endpoint of your RDS instance from the RDS Dashboard.
Connect to the RDS instance using the MySQL client:
mysql -h your-rds-endpoint -u admin -p
Replace
your-rds-endpoint
with the endpoint of your RDS instance. Enter the master password when prompted.
Verify Connection:
Once connected, you can run SQL commands to verify the connection:
SHOW DATABASES;
Conclusion
By completing these tasks, you have successfully set up an RDS instance, launched an EC2 instance, created and assigned an IAM role, and connected your EC2 instance to the RDS instance using a MySQL client. These steps are fundamental for managing relational databases in the cloud and securely connecting your applications to them.
Subscribe to my newsletter
Read articles from Urvish Suhagiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Urvish Suhagiya
Urvish Suhagiya
Exploring the world of DevOps ๐.