> ## Documentation Index
> Fetch the complete documentation index at: https://cloud-architect.ipoint-labs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connecting RDS from EC2 Instance

> Hands-on lab · 55m

Launch this lab in the IP Lab Portal, then follow the steps below in the AWS console.

<a className="ip-lab-portal-btn" href="https://labs.intellectualpoint.com/labs/connecting-rds-from-ec2-instance-2" target="_blank" rel="noreferrer">
  Open IP Lab Portal
</a>

## Overview

### 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) us-east-1.**

### Architecture Diagram

<img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/001.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=1c4f79244c3c9a369d2d44fb7268e93b" alt="" width="1600" height="1250" data-path="images/labs/connecting-rds-from-ec2-instance-2/001.jpg" />

### Task Details

1. Sign in to AWS Management Console
2. Create an EC2 instance.
3. Create an Amazon RDS instance.
4. Create a connection to the Amazon RDS database from the EC2 instance.
5. Create a Database, Table, and insert data for testing.
6. Deleting AWS resources.

### Launching Lab Environment

1. To launch the lab environment, Click on the **Start Lab** button.
2. Please wait until the cloud environment is provisioned. It will take less than a minute to provision.
3. Once the Lab is started, you will be provided with **IAM user name**, **Password**, **Access** **Key**, and **Secret** **Access** **Key**.

**Note** : You can only start one lab at any given time

## Lab guide

#### Lab Steps

#### Task 1: Sign in to AWS Management Console

1. Click on the **Open Console** button, and you will get redirected to AWS Console in a new browser tab.
2. On the AWS sign-in page,

   * Leave the Account ID as default. Never edit/remove the 12-digit Account ID present in the AWS Console. Otherwise, you cannot proceed with the lab.
   * Now copy your **Username** and **Password** in the Lab Console to the **IAM Username and Password** in AWS Console and click on the **Sign-in** button.
3. Once Signed In to the AWS Management Console, make the default AWS Region as **US East (N. Virginia) us-east-1.**

> **Note :** If you face any issues, please go through [**FAQs and Troubleshooting for Labs**](https://play.whizlabs.com/site/task_support/faqs-and-troubleshooting).

#### Task 2: Launch EC2 Instance

1. Make sure you are in  **US East (N.Virginia) us-east-1**  Region.

2. Navigate to EC2 by clicking on the **Services** menu in the top, then click on the **EC2** in the **Compute** section.

3. Navigate to **Instances** on the left panel and click on **Launch instances.**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/002.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=7a9281ed7f2a2216f8d2679a51adb1d7" alt="" width="2417" height="487" data-path="images/labs/connecting-rds-from-ec2-instance-2/002.jpg" />

4. Name : Enter **MyPublicServer**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/003.png?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=2f24bcfa13413beedccc7681cab946ec" alt="" width="967" height="202" data-path="images/labs/connecting-rds-from-ec2-instance-2/003.png" />

5. **For Amazon Machine Image (AMI):** Search for **Amazon Linux 2023** **kernel-6.1** **AMI** in Quick Start menu.

6. For Instance Type: select **t2.micro**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/004.png?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=64d66fc7c5f5c61c604f2fb34509b8b3" alt="" width="1177" height="620" data-path="images/labs/connecting-rds-from-ec2-instance-2/004.png" />

7. **For Key pair:** Select **Create a new key pair** Button

   * Key pair name: Enter **WhizKey**
   * Key pair type: Select **RSA**
   * Private key file format: Select **.pem**

8. Select **Create key pair** Button.

9. In Network Settings, Click on **Edit**:

* Auto-assign public IP: **Enable**
* Select **Create new Security group**
* Security group name : Enter **MyEC2Server\_SG**
* Description : Enter **Security Group to allow traffic to EC2**

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/005.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=9538dbc464abe295842b2c98611395fa" alt="" width="1775" height="735" data-path="images/labs/connecting-rds-from-ec2-instance-2/005.jpg" />

10. To add **SSH:**

    * Choose Type: **SSH**
    * Source: **Anywhere**

11. For **RDS:**

    * Click on **Add security group role**
    * Choose Type: **MySQL/Aurora**
    * Source: **Anywhere**

      <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/006.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=91fe2aa9ec7bdf57cb325702c1bec381" alt="" width="1879" height="1192" data-path="images/labs/connecting-rds-from-ec2-instance-2/006.jpg" />

12. Click on Advanced Details. Under the User data section, enter the following script, (which installs MySQL):

    ```
    #!/bin/bash
    # Download the MySQL repository package
    wget https://dev.mysql.com/get/mysql80-community-release-el9-5.noarch.rpm
    # Install the MySQL repository package
    sudo dnf install mysql80-community-release-el9-5.noarch.rpm -y
    # Enable the MySQL community repository
    sudo dnf repolist enabled | grep "mysql.*-community.*"
    # Install MySQL
    sudo dnf install mysql -y
    ```

13. Keep rest thing Default and Click on **Launch Instance** Button.

14. Select **View all Instances** to View the Instance you Created

15. **Launch Status:** Your instance is now launching. Click on the instance ID and wait for complete initialization of the instance (until the status changes to running).

#### Task 3: Create an Amazon RDS Database

1. Navigate to **Aurora and RDS** under **Database.**
2. Click on **Create Database** button
3. **Specify DB Details**:

   * Database creation method : Select **Standard create**

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/007.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=434f102673821018aa94f52223396c5d" alt="" width="2944" height="456" data-path="images/labs/connecting-rds-from-ec2-instance-2/007.jpg" />

* Engine options : Select **MySQL**
* Version : **Default**

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/008.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=ce4773bb5a8dcf02d66872692794bb16" alt="" width="2462" height="1437" data-path="images/labs/connecting-rds-from-ec2-instance-2/008.jpg" />
* Templates : Select **Sandbox**

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/009.png?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=0fa2c104068198f3f4a83d338c40c4d9" alt="" width="1821" height="202" data-path="images/labs/connecting-rds-from-ec2-instance-2/009.png" />
* DB instance identifier : Enter **mydbinstance**
* Master username : Enter **rdsuser**
* Master password and Confirm password: Enter **whizlabs123**
* **Note:** This is the username/password combo used to log onto your database. Please make note of them somewhere safe.
* DB instance class : **db.t3.micro — 1 vCPU, 1 GiB RAM**

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/010.png?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=6fcfc0abaaf3e388c7da695c1fb51803" alt="" width="770" height="415" data-path="images/labs/connecting-rds-from-ec2-instance-2/010.png" />
* Storage type : **General Purpose SSD (gp2)**
* Allocated storage : **20**
* Enable storage autoscaling : Uncheck

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/011.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=88512e1dbdb907d3edc27d95c34dc82b" alt="" width="1819" height="792" data-path="images/labs/connecting-rds-from-ec2-instance-2/011.jpg" />
* Public access : Select **No**
* VPC Security groups : Select **Choose existing**
* Existing VPC security groups : **Remove the default one and Select MyEC2Server\_SG**
* Go to Additional Configuration options

  * Initial database name: Enter **mydbinstance**
  * DB parameter group: **default**
  * Option group: **default**
  * Enable automated backups: **uncheck**
  * Log Exports: **Not needed for the purpose of this lab.**
  * **Note: Leave all the other settings as default**
* Click on **Create database**

4. Navigate to **databases.**
5. On the RDS console, the details for the new DB instance appear. The DB instance has a status of creating until the DB instance is ready to use. When the state changes to **Available**, you can connect to the DB instance. It can take up to 20 minutes before the new instance status becomes **Available**.
6. Once the database becomes **Available**, click on the database name and copy the **Endpoint** under **Connectivity & security** tab.

#### Task 4: Create a connection to the Amazon RDS database from the EC2 instance

In this task, we are going to establish a connection between the EC2 instance and the RDS database.

1. Now navigate to EC2 by clicking on the **Services** menu in the top, then click on the **EC2** in the **Compute** section.

2. Navigate to **Instances** on the left panel and select your EC2 instance(**MyPublicServer**) and click on the **Connect** button.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/012.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=22313584c27fcefb0b979757ee7689ed" alt="" width="2448" height="388" data-path="images/labs/connecting-rds-from-ec2-instance-2/012.jpg" />

3. Select **EC2 Instance Connect** option and click on **Connect** button.(Keep everything else as default)

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/013.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=cbd4f645675e5042669c6a22af70e891" alt="" width="2943" height="1225" data-path="images/labs/connecting-rds-from-ec2-instance-2/013.jpg" />

4. A new tab will open in the browser where you can execute the CLI Commands.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/014.png?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=9c091e8b971575ec0761306edcb1e394" alt="" width="830" height="297" data-path="images/labs/connecting-rds-from-ec2-instance-2/014.png" />

5. Once connected to the server:

   * Switch to the root user using the command:

     ```
     sudo su
     ```

6. Connect to the MySQL RDS Instance with the following command:

   * Syntax: **mysql -h** \<mysql-instance-dns> **-u** \<username> **-p**
   * In our case: **mysql -h** mydbinstance.c1sh5a4j91dp.us-east-1.rds.amazonaws.com **-u** rdsuser **-p**
   * Password : Enter **whizlabs123**

7. You will enter the MYSQL command line.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/015.png?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=cd625bd828b69c79405474b62b1ddb32" alt="" width="1246" height="354" data-path="images/labs/connecting-rds-from-ec2-instance-2/015.png" />

#### Task 5: Create a Database, Table and insert data for testing

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;
  ```

<img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/016.png?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=3b83232609da85b93b8aaf3c127d0159" alt="" width="456" height="393" data-path="images/labs/connecting-rds-from-ec2-instance-2/016.png" />

* 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;
  ```
* 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;
  ```

<img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/017.png?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=f31149f248e83bd723ef4c6fd48047e5" alt="" width="490" height="172" data-path="images/labs/connecting-rds-from-ec2-instance-2/017.png" />

* Try out some more SQL commands and play around with the table to strengthen your understanding.
* Run below command to exit the mysql command

  ```
  exit;
  ```

> ##### Do You Know?
>
> The combination of Amazon RDS and Amazon EC2 provides a powerful and scalable infrastructure for building robust and high-performing applications.

1. Once the lab steps are completed, please click on the **Validation** button on the left side panel.
2. This will validate the resources in the AWS account and shows you whether you have completed this lab successfully or not.

#### Task 6: Delete AWS Resources

##### 7.1 Deleting EC2 Instance

1. Navigate to EC2 by clicking on the **Services** menu in the top, then click on the **EC2** in the **Compute** section.
2. All the EC2 Instances will be listed here,

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/019.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=6f329f32f17561ddf31fc5a0eeeb5a5c" alt="" width="2452" height="329" data-path="images/labs/connecting-rds-from-ec2-instance-2/019.jpg" />
3. To terminate the EC2, perform the following task:

   * Select **MyPublicServer**,
   * Click on Instance state (If you don't see an **Instance state** option, click on the **Actions** button and then choose **Instance state**)
   * Select **Terminate instance**

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/020.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=db19244f7d3344a30f90a5f202d848e6" alt="" width="2440" height="396" data-path="images/labs/connecting-rds-from-ec2-instance-2/020.jpg" />
4. Finally, Click on terminate

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/021.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=5a91fd3b531b09b60205ab982b79a76d" alt="" width="1639" height="618" data-path="images/labs/connecting-rds-from-ec2-instance-2/021.jpg" />
5. EC2 Instance is terminated.

##### 7.2 Deleting RDS DB Instance

1. Navigate to **RDS** by clicking on the **Services** menu available under the **Database** section.
2. Click on DB Instances
3. It will list all the RDS databases
4. Click on **Actions** and select **Delete**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/022.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=07d94791d077b6b1dfe0af7bebb92566" alt="" width="2377" height="585" data-path="images/labs/connecting-rds-from-ec2-instance-2/022.jpg" />
5. To delete, we have to perform several tasks:

   * Uncheck the option of **Create final snapshot**,
   * Acknowledge by **selecting** the second option,
   * Type **delete me** to confirm
   * And finally, click on **Delete** button below.

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/BhmD8yWhz4o65hU3/images/labs/connecting-rds-from-ec2-instance-2/023.jpg?fit=max&auto=format&n=BhmD8yWhz4o65hU3&q=85&s=83f95cbeeb79eaa6f3d2409f9d0409be" alt="" width="1190" height="1165" data-path="images/labs/connecting-rds-from-ec2-instance-2/023.jpg" />
6. It will take around, 5 minutes to delete the instance, you can end the lab now.

### Completion and Conclusion

1. You have successfully launched an EC2 Instance in a default VPC.
2. You successfully ran a MySQL command and performed operations on a database created with Amazon RDS.

### End Lab

1. Sign out of AWS Account.
2. You have successfully completed the lab.
3. Once you have completed the steps, click on **End Lab** from the IP Lab Portal dashboard.

## What gets checked

When you press **Check my work**, the platform verifies each of these:

* **Launch an EC2 Instance** — Check whether an EC2 Instance is launched or not.
* **Launch EC2 AMI type Amazon Linux** — Check whether the EC2 instance is launched using an Amazon AMI.
* **Launch RDS instance** — Check whether an RDS Instance is created or not
* **Check whether RDS database Engine type is mysql** — Check whether allowed database type mysql is deployed or not

## Related help

* [FAQs and Troubleshooting](/aws-saa/support/faqs-and-troubleshooting)
* [SSH into EC2 Instance](/aws-saa/support/ssh-into-ec2-instance)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.