Amazon Web Services(AWS) provides a range of cloud-based services, and two of the most popular are Amazon Elastic Compute Cloud(EC2) and Amazon Relational Database Service (RDS). EC2 is a virtual server running in the cloud that can be scaled up or down as per the needs of a user, while RDS is a service that enables us to setup, operate and scale a relational database in the AWS cloud.
In this blog, we will go through the steps to launch an EC2 instance, launch an RDS instance, and connect RDS from the EC2 instance.
Configuring an EC2 instance
1. Sign in to the AWS Management Console and navigate to the EC2 service.
2. Click on the “Launch Instance”
3. Name your instance,and choose the Amazon Machine Image(AMI)
for the operating system image
4. Select t2.micro
as an Instance type
5. Now let’s create a key pair that will allow us to connect with our EC2 instance.
6. To control the traffic for our instance we have to configure the security groups. We will allow SSH
to this
instance and HTTP
and HTTPS
traffic from the internet.
7. Click on the launch instance button and our EC2 machine will be up and running in a couple of minutes
Configuring RDS
1. Search for RDS
in the management console and navigate to it.
2. Click on the Create database button.
3. Select the Easy create
option and choose MySQL
from the engine type
4. Choose Free tier
from the available instance sizes and enter a name for the database instance, master username, and master password. Record the username and master password somewhere as it will be used for connecting with our EC2 instance.
5. Click on the Create database
button. It will take a couple of minutes for MySQL instance status to be available.
Connecting EC2 instance and DB instance
AWS provides us the option to automatically connect the EC2 instance with the DB instance. To connect our EC2 instance with the DB instance
1. Select the DB identifier and click on Set up EC2 connection from the Action menu
2. Now, select the EC2 instance that we created earlier and click on Continue
3. Click on Confirm and set up
Connecting to a MySQL DB instance
Now we will connect to the EC2 instance that we created earlier and then install the required packages to connect with our MySQL DB instance.
1. To connect with our EC2 instance copy the Public IPv4 address of the EC2 machine
2. Open the terminal and navigate to the location where we have stored the key pair
file.
3. Enter the following command to modify the .pem
file permission. Replace the aws-key
with the key created while configuring the EC2 instance.
4. Enter the following command to connect with our instance. Here ec2-user
is the default user in AMI OS.
5. Now to install the MySQL command line client run the following commands. Press Y
when prompted
6. To connect with the DB instance copy the endpoint
from the Connectivity & security
tab on the DB-identifier page
7. Enter the following command to connect with the MySQL instance. Replace the endpoint
with the endpoint of the DB instance
and enter the master password when prompted for password
We have now successfully connected our EC2 instance with the RDS DB instance. To verify this we can run some of the MySQL commands