Set Up a Free Tier RDS Instance of MySQL and Connect with EC2

In previous blogs, we have dealt with the AWS CLI and accessing S3 and IAM programmatically through CLI. In this blog let us understand the RDS in AWS.

Here's the official documentation for Relational Database Service in AWS.

RDS (Relational Database Service) in AWS

According to the official documentation, "Amazon Relational Database Service (Amazon RDS) is a web service that makes it easier to set up, operate, and scale a relational database in the AWS Cloud. It provides cost-efficient, resizable capacity for an industry-standard relational database and manages common database administration tasks."

RDS offers six database engines:

  • MySQL: A widely-used open-source relational database management system known for its stability, ease of use, and compatibility.

  • MariaDB: A community-developed, open-source fork of MySQL, offering similar features and compatibility.

  • PostgreSQL: An open-source object-relational database management system known for its robustness, extensibility, and support for advanced features.

  • Oracle: A commercial relational database management system that provides enterprise-level features and capabilities.

  • SQL Server: A commercial relational database management system developed by Microsoft, offering scalability, security, and integration with Microsoft products.

  • Amazon Aurora: A MySQL and PostgreSQL-compatible database engine developed by AWS. It offers high performance, scalability, and durability. It is up to five times faster than MySQL.

Some more links that can help:

Let us jump into today's tasks to get our hands dirty and learn some fun things.

Task:

Create a Free tier RDS instance of MySQL.

Once the RDS instance is up and running, get the credentials and connect your EC2 instance using a MySQL client.

Create an EC2 instance

I am creating an instance named database.

I am configuring the security group to allow inbound traffic on the MySQL port (default is 3306). I am keeping all traffic open.

Keep the configuration as a free tier to avoid any charges.

Create a Free tier RDS instance of MySQL

Go to your AWS console > Search RDS > Click Create database.

Select Standard Create > Under "Engine options", choose "MySQL"

Select the Free tier

Enter a unique name for the "DB instance identifier".(default database-1)

Set the "Master username" and "Master password" for the database.

Set the "Virtual Private Cloud (VPC)" and "Subnet group" to create the instance.

Leave the other settings at their default values.

Under Settings, I am providing the following details:

DB instance identifier: database

Credentials Settings:

Master username: admin

And provide the password of your choice according to the constraints mentioned.

Configure other settings like storage, backups, VPC, and security groups according to your requirements. Review the configuration and click "Create Database".

RDS creation takes a few minutes.

Select Connect to an EC2 compute resource > EC2-instance.

The database is created.

Assign the role to EC2 so that your EC2 Instance can connect with RDS.

Go to IAM Dashboard > Click on Roles > Create Role.

Select the EC2 service for the trusted entity > Use case EC2

In permission policies, attach the permission AmazonRDSFullAccess

Name the role as "database_user".

Click on Create Role and the role is created.

You can check the details of the database.

Let's assign this role to our EC2 which we created.

Go to your EC2 Management Console > Actions > Security > Modify IAM Roles

Select the role we created for this task and click "Update IAM role".

The instance will successfully modify the IAM Role.

Once the RDS instance is up and running, get the credentials and connect your EC2 instance using a MySQL client.

Go to RDS Dashboard > Select the Database you created > Copy the endpoint, port, and master username.

Under Connectivity & security:

Endpoint:

Port: 3306

Under Configuration:

Master username: admin

Let's connect to the EC2 instance using SSH.

And then install the MySQL client in the instance:

sudo apt-get update
sudo apt-get install mysql-client -y
mysql --version

To connect to the RDS instance using the MySQL client and the endpoint address, username, and password, we use the following command:

mysql -h <RDS_ENDPOINT> -P <RDS_PORT> -u <MASTER_USERNAME> -p
#The below details we copied when we created the RDS instance:
#<RDS_ENDPOINT> with the endpoint of your RDS instance
#<RDS_PORT> with the port number (default is 3306)
#<MASTER_USERNAME> with the master username
# '-h' is used to specify the endpoint of MySQL server to which we want to connect (basically the host)

My command will look like this:

After running this command, you will be prompted for the password. Give the password you created while creating RDS:

Now you can verify the CRUD operations confirming the MYSQL is working properly.

And Yay! We have created a Free tier RDS instance of MySQL, an EC2 instance, assigned an IAM role with RDS access to the EC2 instance, and connected to the RDS instance from the EC2 instance using a MySQL client.


In this blog, I have discussed Relational Database Services in AWS. If you have any questions or want to share your experiences, please comment below. Don't forget to read my blogs and connect with me on LinkedIn and let's have a conversation.

In the next blog post, we will explore more advanced topics in the realm of DevOps.So, stay tuned and let me know if there is any correction.