Connecting AWS RDS with an EC2 instance

Nipulpatel
5 min readSep 12, 2023

--

What Is Amazon RDS?

Amazon Relational Database Service (RDS) is a web service provided by Amazon Web Services (AWS) that makes it easy to set up, operate, and scale a relational database in the cloud. It is a fully-managed database service that supports various popular relational database engines such as MySQL, PostgreSQL, Oracle, and SQL Server .

You can launch any number of databases within minutes using point-and-click management tools or AWS Management Console commands. The service automatically scales as usage increases; you pay only for what you use when your applications need more processing power or storage capacity.

How does AWS RDS work?

AWS RDS automatically creates, configures, and manages multiple instances of MySQL, Oracle, SQL Server, or PostgreSQL for you. You can create a Multi-AZ deployment for high availability or read replicas for scalability.

While automating time-consuming administrative tasks, Amazon RDS provides cost-effective and scalable capacity. Such as hardware provisioning, setup and configuration, backup and recovery, patching, monitoring, and logging.

we will go through the steps to launch an EC2 instance, launch an RDS instance, and connect RDS from the EC2 instance.

Create an EC2 instance

  • Go to the Amazon EC2 console.
  • Click “Launch Instance”.
  • Choose a Linux AMI.
  • Choose an instance type, such as t2.micro.
  • Choose a VPC and subnet.

Configure security group rules to allow inbound traffic on the appropriate port for the type of database you are using (e.g. port 3306 for MySQL).

  • Launch the instance.

- Create a Free tier RDS instance of MySQL

  • Navigate to the RDS section in the AWS management console and click to Create a database.
  • Select the Engine option which is the type of database to setup. We will select MySQL as per our task.
  • Select free-tier as the template.
  • Set up the DB credentials.
  • Select the db size.
  • Select the EC2 Compute connect as we are going to access our database from EC2.
  • Choose the VPC that you have created already while spinning EC2 instance.
  • Use Password authentication to access the database.
  • Now the new database is create

- Create an IAM role with RDS access

  • Go to the IAM console. Click “Roles”. Click “Create role”.
  • Choose the ‘AWS service’.
  • Choose “Allows EC2 instances to call AWS services on your behalf”.
  • .Attach the “AmazonRDSFullAccess” policy.
  • Enter a unique name for the role and Click on “Create Role”.
  • Role is Created successfully.

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

  • Go to the EC2 console.
  • Select the instance you just created.
  • Click “Actions”, then “Security”, then “Modify IAM Role”.
  • Choose the IAM role you just created.
  • Click “Update IAM role”.

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

  • Go to the RDS console.
  • Select the instance you just created.
  • Click “Configuration” and note the endpoint address.
  • SSH into your EC2 instance using a terminal or remote access tool.
  • Install a MySQL client, such as “mysql”.
  • Command for install mysql-client.
  • sudo apt-get install mysql-client
  • Connect to the RDS instance using the MySQL client and the endpoint address, username, and password:
mysql -h <endpoint address> -P <port.no> -u <username> -p
  • Enter the password when prompted and press enter.
  • You should now be connected to the MySQL database on the RDS instance.

Conclusion

AWS RDS makes it easy to manage your database and keep it up to date with the most delinquent security patches and software updates. It also allows you to easily migrate your database from one region to another, which is excellent if you need to move your data closer to your customers or employees. It offers an easy way to set up an instance with a MySQL database engine of your choosing; then you can simply scale up or down as needed based on how much capacity you require at this moment in time.

--

--

No responses yet