Notice: Trying to get property 'post_excerpt' of non-object in /home/n3svtp4r09fz/technet.vn/wp-content/themes/darknews/single.php on line 43
Lab Details
- This lab walks you through the steps of connecting Amazon EC2 with Amazon RDS Instance.
- We will create an EC2 instance inside a public subnet and an Amazon RDS in a private subnet group.
- Duration: 55 minutes
- AWS Region: US East (N. Virginia)
Tasks
- Log into the AWS Management Console.
- Create an EC2 instance.
- Create an Amazon RDS instance.
- Create a connection to the Amazon RDS database on the EC2 instance.
- 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
- Network : Select Default VPC
- 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
- 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).
- Choose Type:
-
For HTTP:
- Click on
- Choose Type: HTTP
- Source:
(Allow specific IP address) or
(From ALL IP addresses accessible).
- Click on
-
For HTTPS:
- Click on
- Choose Type: HTTPS
- Source:
(Allow specific IP address) or
(From ALL IP addresses accessible).
- Click on
- After that click on
- Assign a security group: Create a new security group
- 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
- In the left navigation pane, click on Databases.
- Click
.
- 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.
- Virtual Private Cloud (VPC) : Select same default VPC which was available while creating EC2
-
Under Database Options,
- Database name : Enter a database name – myrdsdatabase
- Leave other parameters as default.
- Database name : Enter a database name – myrdsdatabase
-
In the Backup section,
- For Backup retention period, select 0 days
- Leave other parameters as default.
- For Backup retention period, select 0 days
- 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.
- Scroll to the bottom of the page, then click
-
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.
- In the left navigation pane, click Databases.
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
- 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.
- The MySQL rule will already exist.
- 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
- Change to root user: sudo su
-
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
- Syntax: mysql -h <<mysql-instance-dns>> -P 3306 -u <<username>>-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;
- CREATE DATABASE SchoolDB;
-
You can see the created database with following command:
- show databases;
- show databases;
-
Switch to the database named SchoolDB.
- use 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’);
- INSERT INTO subjects(subject_name, teacher) VALUES (‘English’, ‘John Taylor’);
-
Let’s check the items we added into the table:
- select * from subjects;
?
- 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
- You have successfully launched EC2 Instance in a default VPC.
- You have successfully launched Amazon RDS and updated the security group so that the EC2 Instance can access the Amazon RDS.
- You successfully ran a MySQL command and performed operations on a database created with Amazon RDS.