AWS LAB

Launch Amazon EC2 instance, Launch Amazon RDS Instance, Connecting RDS from EC2 Instance

Lab Details

  1. This lab walks you through the steps of connecting Amazon EC2 with Amazon RDS Instance.
  2. We will create an EC2 instance inside a public subnet and an Amazon RDS in a private subnet group.
  3. Duration: 55 minutes
  4. AWS Region: US East (N. Virginia)

Tasks

  1. Log into the AWS Management Console.
  2. Create an EC2 instance.
  3. Create an Amazon RDS instance.
  4. Create a connection to the Amazon RDS database on the EC2 instance.
  5. Create a Database and Add new tables and data to Database for testing.

Architecture Diagram

Lab Steps

Launch EC2 Instance

  • Make sure you are in N.Virginia Region.
  • Navigate to EC2 by clicking on the  menu in the top, then click on the  in the  section.
  • Navigate to on the left panel and click on 
  • Choose an Amazon Machine Image (AMI): Search for Amazon Linux 2 AMI in the search box and click on the select button.

  • Choose an Instance Type: Select  and then click on  .
  • Configure Instance Details:
    • Network        : Select Default VPC
    • Subnet         : Default selected
    • Auto-assign Public IP     Enable – It should be enabled as a public IP is needed for connecting to EC2 via SSH.
    • Leave everything else as default and click on 
  • Add Storage Page : No need to change anything in this step. Click on .
  • Add Tags Page
    • Click on 
    • Key    : Enter Name
    • Value    : Enter MyPublicServer
    • Click on 
  • On the Configure Security Group page:
    • Assign a security group: Create a new security group
    • Security group name: PublicEC2_SG
    • Description: PublicEC2_SG
    • To add SSH:
      • Choose Type: 
      • Source: Custom (Allow specific IP address) or Anywhere (From ALL IP addresses accessible).
    • For HTTP:
      • Click on 
      • Choose Type: HTTP
      • Source: (Allow specific IP address) or  (From ALL IP addresses accessible).
    • For HTTPS:
      • Click on 
      • Choose Type: HTTPS
      • Source: (Allow specific IP address) or  (From ALL IP addresses accessible).
    • After that click on 
  • Review and Launch : Review all your settings and click on .
  • Key Pair – Create a new key pair and click on  to save it to your local machine with the key pair name as MyKey.
  • Once the download is complete, click on .
  • After 1-2 minutes, the Instance State will change to running.

Create an Amazon RDS Database

  1. In the left navigation pane, click on Databases.
  2. Click .
  3. Click on Switch to your original interface


  • Note: Make sure Only enable options eligible for RDS Free Usage Tier is checked If not, some configurations which are not part of the free tier will not work and you will face issues.


  • Select MySQL. Click 
  • License model            : general-public-licence
  • DB engine version         : leave the default
  • DB instance class        : db.t2.micro – 1 vCPU, 1 GiB RAM.
  • Allocated Storage        : 20 GIB
  • Enable storage autoscaling    : uncheck
  • In the Settings section, configure,
  • DB instance identifier        : mydbinstance
  • Master username        : Enter rdsuser
  • Master password        : Enter a password and note it down – whizlabs123
  • Confirm password        : Confirm the password.
  • Click .
  • Note: Make sure you record all the details you entered , including the DB Instance Identifier, Username, Password etc.. They will be used while connecting from EC2.
  • Under Configure advanced settings, in the Network Security section, configure the following:
    • Virtual Private Cloud (VPC)    : Select same default VPC which was available while creating EC2
    • Subnet Group             : default
    • Public accessibility        : No
    • VPC security groups        : Create new VPC security group
    • Leave other parameters as default. 
  • Under Database Options,
    • Database name         : Enter a database name – myrdsdatabase
    • Leave other parameters as default.
  • In the Backup section,
    • For Backup retention period, select 0 days
    • Leave other parameters as default.
  • Enable deletion protection     : uncheck
  • Leave other parameters as default.
    • Scroll to the bottom of the page, then click .
    • Click  to see the RDS Instance created.
  • It will take a few minutes for the MySQL database to become available.
    • In the left navigation pane, click Databases.
    • Click refresh every 60 seconds until the instance status changes to available.

Connect Public EC2 Server to RDS Database

In this task, you will connect Public Server to RDS database (in your Private subnet).

Configure Database Security Group

  • Find the MySQL Database Endpoint. To locate it, click on mydbinstance. Navigate to Connectivity & security. Under EndPoint & port, you will find the Endpoint.
  • Copy the Endpoint to your clipboard. You RDS endpoint should look similar to:
    • mydbinstance.cdegnvsebaim.us-east-1.rds.amazonaws.com
  • Under Security, click on the VPC security group shown.
  • It will open the Security Group page. Click on InBound.
    • The MySQL rule will already exist.
    • Under source, delete the IP Address and type sg. This shows the list of security groups available.


  • Select the PublicEC2_SG.
  • Click on .

SSH into EC2 and Connect to Your Database

  • Follow the steps in SSH into EC2 Instance.
  • Once connected to the server:
    • Change to root user: sudo su
    • Install MySQL : yum install mysql
  • Connect to the MySQL RDS Instance with following command:
    • Syntax: mysql -h <<mysql-instance-dns>> -P 3306 -u <<username>>-p
    • In our case: mysql -h mydbinstance.cdegnvsebaim.us-east-1.rds.amazonaws.com -P 3306 -u rdsuser -p
  • Provide the password which was created during instance creation.
  • You will enter the MYSQL command line.
  • Lets create a simple database and table to see if it’s working.
    • Create a database:
      • CREATE DATABASE SchoolDB;
    • You can see the created database with following command:
      • show databases;
    • Switch to the database named SchoolDB.
      • use SchoolDB;
    • Create a sample table consisting of Subjects.
      • CREATE TABLE IF NOT EXISTS subjects ( 

        subject_id INT AUTO_INCREMENT,

                subject_name VARCHAR(255) NOT NULL,

                teacher VARCHAR(255),

                start_date DATE,

                lesson TEXT,

                PRIMARY KEY (subject_id)

      ) ENGINE=INNODB;

  • Enter show tables; to see the table you just created.
  • Insert some details into the table:
    • INSERT INTO subjects(subject_name, teacher) VALUES (‘English’, ‘John Taylor’);
    • INSERT INTO subjects(subject_name, teacher) VALUES (‘Science’, ‘Mary Smith’);
    • INSERT INTO subjects(subject_name, teacher) VALUES (‘Maths’, ‘Ted Miller’);
    • INSERT INTO subjects(subject_name, teacher) VALUES (‘Arts’, ‘Suzan Carpenter’);
  • Let’s check the items we added into the table:
    • select * from subjects;

    ?

  • Try out some more SQL commands and play around with the table to strengthen your understanding..
  • Run exit; to exit the MySQL client.

Completion and Conclusion

  1. You have successfully launched EC2 Instance in a default VPC.
  2. You have successfully launched Amazon RDS and updated the security group so that the EC2 Instance can access the Amazon RDS.
  3. You successfully ran a MySQL command and performed operations on a database created with Amazon RDS.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: