How to Connect AWS RDS from Any Linux Server

Sarvar
6 min readJul 7, 2023

--

Hey,

My name is Sarvar, and I am working as Senior Developer at Luxoft India. With years of experience working on cutting-edge technologies, I have honed my expertise in Cloud Operations (Azure and AWS), Big Data, and DevOps. Throughout my career, I’ve worked with clients from all around the world, delivering excellent results, and going above and beyond expectations. I am passionate about learning the latest and treading technologies.

Today we are looking into one of the most popular cloud-based database services provided by AWS, called Amazon Relation Database Service, or AWS RDS. We are looking at some of the basic theory behind the Amazon relational database service. What are the different types of databases offered by Amazon RDS? Later, we will look at how to create Amazon RDS using Terraform, and we will connect Amazon RDS through Amazon EC2 Instance.

What is Amazon RDS:

A fully managed database service, Amazon RDS (Relational Database Service) is provided by Amazon Web Services (AWS). It provides a simple method for setting up, running, and scaling relational databases on the cloud. The administrative duties related to database management, such as hardware provisioning, database setup, backups, patching, and automated software upgrades, can be delegated using RDS. This enables you to concentrate on the creation and management of your applications.

Amazon RDS

The fact that Amazon RDS supports a variety of well-liked database engines is one of its main benefits. Amazon Aurora, MySQL, PostgreSQL, MariaDB, Oracle Database, and Microsoft SQL Server are just a few of the possibilities available. You can choose the database engine that best suits the needs of your application thanks to this flexibility. Amazon RDS saves you time and effort by handling a variety of administrative responsibilities because it is a managed service. Database upkeep, automatic backups, software patches, failover detection, and recovery are all handled by AWS. As a result, you can focus on creating and improving your apps rather than worrying about managing the underlying infrastructure.

Let’s Create AWS RDS Database Using Terraform:

Please execute the commands “terraform init” and “terraform validate” to set up your Terraform setup and check for syntax and configuration problems, respectively. Please run “terraform plan” to review the suggested changes to your infrastructure once you have copied the below template and created the.tf file. The RDS instance will then be created depending on your Terraform setup by running “terraform apply” after that.

Terraform Teamplate for Amazon RDS:

provider "aws" {
region = "us-east-1" # Change AWS region
}

resource "aws_db_instance" "rds_instance" {
identifier = "my-rds"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro" # Instance type
allocated_storage = 10 # Size in GB

name = "myRDS" # Database name
username = "Sarvar" # Username
password = "password" # Password

vpc_security_group_ids = ["sg-12345678"] # Replace with your security group IDs

}
# If you want to get out after tf file execution please add below lines
output "rds_endpoint" {
value = aws_db_instance.rds_instance.endpoint
description = "Endpoint URL of the RDS Databases"
}

Once you click on terraform apply just type Yes and here you go now Amazon RDS instance is created successfully. now we will look at how to connect amazon RDS using Amazon EC2 Instance.

RDS Instance Created Successfully using Terraform

Let's Connect Amazon RDS using Amazon EC2:

For connecting Amazon RDS, we have a number of options, but one of the easiest ways to connect Amazon RDS is using any Linux-based server. Just run two-to-three commands, and here you go, you are connected. Please follow the below steps to establish the connection on Amazon EC2.

Amazon RDS Dashboard
  1. Install the MySQL Client library. this library will help you connect to remote databases.
sudo yum install mysql -y

2. Now Now is the moment of truth! Just get the endpoint details from the Terraform output widget and paste that output in the below command. Provide the user name, in my case, Sarvar, and hit enter.

mysql -h <endpoint> -u <username> -p

You will then be prompted to enter the password associated with your RDS. If successful, the Welcome message will appear.

Basic MySQL Command Lines:

  1. Type to show databases:
mysql> show databases;

2. Type to create new database:

mysql> create database luxoft;

3. Type to show all the tables:

mysql> show tables;

4. Using the commands shown below, create a table in the Luxoft database, and then enter the following data into the employee table:

mysql> use luxoft;
CREATE TABLE employees (
employee_id INT,
first_name VARCHAR(20),
last_name VARCHAR(20),
email VARCHAR(50),
hire_date DATE,
address VARCHAR(120),
phone_number VARCHAR(15)
);
INSERT INTO employees (employee_id, first_name, last_name, email, hire_date, address, phone_number)
VALUES
(1, 'Josh', 'Done', 'john.done@example.com', '2020-01-01', '123 Main St', '555-123-4567'),
(2, 'Jane', 'Smith', 'jane.smith@example.com', '2020-02-15', '456 Elm St', '555-987-6543'),
(3, 'David', 'Johnson', 'david.johnson@example.com', '2020-03-10', '789 Oak St', '555-789-1234'),
(4, 'Sarah', 'Williams', 'sarah.williams@example.com', '2020-04-05', '321 Maple Ave', '555-456-7890'),
(5, 'Michael', 'Brown', 'michael.brown@example.com', '2020-05-20', '654 Pine Rd', '555-321-0987'),
(6, 'Emily', 'Jones', 'emily.jones@example.com', '2020-06-12', '987 Cedar Ln', '555-654-3210'),
(7, 'Daniel', 'Taylor', 'daniel.taylor@example.com', '2020-07-18', '852 Walnut St', '555-012-3456'),
(8, 'Olivia', 'Davis', 'olivia.davis@example.com', '2020-08-24', '369 Birch Dr', '555-789-0123'),
(9, 'James', 'Wilson', 'james.wilson@example.com', '2020-09-30', '147 Pineapple Ave', '555-456-7890'),
(10, 'Sophia', 'Anderson', 'sophia.anderson@example.com', '2020-10-15', '963 Orange Ct', '555-123-4567');
SELECT * FROM employees;

Conclusion: In summary, Amazon RDS provides scalability, high availability, security, and automation while making it easier to maintain and run relational databases in the cloud. It is a strong service that allows you to run and maintain your database architecture on AWS without having to have a lot of database administration experience.

Here is the End!

Thank you for taking the time to read my article. I hope you found this article informative and helpful. As I continue to explore the latest developments in technology, I look forward to sharing my insights with you. Stay tuned for more articles like this one that break down complex concepts and make them easier to understand.

Remember, learning is a lifelong journey, and it’s important to keep up with the latest trends and developments to stay ahead of the curve. Thank you again for reading, and I hope to see you in the next article!

Happy Learning!

--

--

Sarvar
Sarvar

No responses yet