> ## 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.

# How to Query into RDS MySQL Using AWS Lambda

> Hands-on lab · 15m

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/how-to-query-into-rds-mysql-using-aws-lambda" target="_blank" rel="noreferrer">
  Open IP Lab Portal
</a>

## Overview

### Lab Details

1. This lab walks you through the creation of an RDS MySQL Database, creating a table and inserting some values using MySQL Workbench. Now we will create a Lambda function and write MySQL queries to read and write data into the database.
2. Duration: **75 minutes**
3. AWS Region: **US East (N. Virginia)** **us-east-1**

### Introduction

1. Creating a MySQL database on Amazon RDS will configure the database instance with the desired specifications such as **instance type, storage size, and security settings.**
2. Connecting to RDS MySQL from Lambda: Within your Lambda function code, use the programming language's MySQL library or a MySQL connector to establish a connection with the RDS MySQL database. Pass the RDS endpoint, username, password, and database name to establish the connection.
3. Test and deploy: Test your Lambda function locally and ensure that it can successfully query the RDS MySQL database. Once you are satisfied with the functionality, deploy the Lambda function to AWS and test it in the production environment. Monitor the logs and performance of your Lambda function to ensure it is functioning as expected.

### Architecture Diagram

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/001.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=37ff8658ff01f578e839c7645a35f1b2" alt="" width="3553" height="1834" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/001.png" />

### Task Details

1. Sign in to AWS Management Console
2. Create a security group for RDS Instance
3. Create RDS Database Instance
4. Connect to the RDS instance using MySQL Workbench
5. Create a Lambda function
6. Deleting AWS Resources

### Prerequisites

1. For testing this lab, it is necessary to download the MySql GUI Tool, To download it, go to the [Download MySQL Workbench page](https://dev.mysql.com/downloads/workbench/). Based on your OS, select the respective option under **Generally Available (GA) Releases**. Download and Install.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/002.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=8e30813d10674ce5b2750aa653dfb9dc" alt="" width="489" height="211" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/002.png" />

### 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 username**, **Password**, **Access** **Key**, and **Secret** **Access** **Key**.

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

## Files you need

Download these before you start — the lab cannot be completed without them.

* [Download Zip](/images/saa-files/how-to-query-into-rds-mysql-using-aws-lambda/download-zip.zip)
* [Lambda code 2](/images/saa-files/how-to-query-into-rds-mysql-using-aws-lambda/lambda-code-2.py)

## 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 **User Name** 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.**

#### Task 2: Create a Security Group for RDS instance

1. Make sure you are in the **N.Virginia us-east-1** Region.

2. Click on **Services** and select **EC2** under the **Compute** section.

3. On the left panel menu, select the **Security Groups** under the **Network & Security** section.

4. Click on the **Create Security Group.**

5. We are going to create a Security group for RDS with a 3306 port number enabled.

   * Security group name : Enter ***RDS\_lab\_sg***
   * Description: Enter ***Security group for RDS instance***
   * VPC: Select Default VPC

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/003.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=34ee204d54e8955bde114e1675d824ed" alt="" width="1002" height="304" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/003.png" />

6. Click on the **Add rule** button under **Inbound rules.**

   * Type: Select **MYSQL/Aurora**
   * Source: Select **Anywhere-IPv4**
   * In the textbox add ***0.0.0.0/0***

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/004.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=55d417b32032434ba1c501b54bc6ab08" alt="" width="1222" height="160" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/004.png" />

7. Leave everything as default and click on the **Create Security Group.**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/005.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=3d485c50a8f9b396622f7e24215e2766" alt="" width="1222" height="458" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/005.png" />

#### Task 3: Create RDS Database Instance

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

2. Navigate to **Aurora and RDS** by clicking on the **Services** menu available under the **Database** section.

3. Click on **Databases** on the left panel and then click on **Create Database**.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/006.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=003c0394237373829842887e84b389dc" alt="" width="1222" height="199" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/006.png" />

* Database creation method : **Standard create**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/007.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=9eab3495c3c75505287a94455d98ca6a" alt="" width="1604" height="292" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/007.png" />

* Engine options : Select **MySQL**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/008.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=866b2c8e041f505c8f1e1d7c8f7deb7f" alt="" width="1222" height="526" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/008.png" />

* Version : **Default**
* Templates : Select **Sandbox**
  **Note: Selecting the Sandbox as a template is compulsory, else database won't be created.**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/009.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=52704ac49773b3083a556ed47779d381" alt="" width="1222" height="134" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/009.png" />

* DB instance identifier : ***mydatabaseinstance***
* Master username: ***mydatabaseuser***
* Credentials management: Select **Self managed**
* Master password and Confirm password: ***mydatabasepassword***

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/010.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=9e0f219e00dafb526dc8de1e8bf2aa0b" alt="" width="1222" height="497" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/010.png" />

* DB instance class : **db.t3.micro — 2 vCPU, 1 GiB RAM**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/011.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=a0d6e6d63734e25c14f52237e82dbdf1" alt="" width="1610" height="780" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/011.png" />

* Storage type : **General Purpose (SSD)**
* Allocated storage : **20 (default)**
* Click on **Additional storage configuration** to expand.
* Enable storage autoscaling : **Uncheck**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/012.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=ec9b111ebd80b7f853d8e457d9032d21" alt="" width="1158" height="822" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/012.png" />

* Public access : Select **Yes**
* VPC security groups **: Select Choose existing**
  **Note: Remove the default one and select RDS\_lab\_sg instead**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/013.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=ced40ec4c77cab6641865044e4a5e3c2" alt="" width="1612" height="852" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/013.png" />

Click on **Additional Configuration** in the bottom of the page to expand it:

* Initial database name: ***mydatabase***
* DB parameter group: **default**
* Option group: **default**
* Enable automated backups: **uncheck**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/014.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=fed9735a6333fe04eead3b81e4b41f09" alt="" width="1740" height="804" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/014.png" />

> **Note: Leave all the other setting as default**

4. Click on **Create database**

5. Navigate to **Databases.**

6. 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**.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/015.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=b40064711386f3bf47f8b5f48fa00f4b" alt="" width="1898" height="350" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/015.png" />

#### Task 4: Connecting to RDS Database on a DB Instance using the MySQL Workbench

One GUI-based application you can use to connect is MySQL workbench, which you have already downloaded and installed based on instructions in the **prerequisite** section.

1. To connect to a database on a DB instance using MySQL monitor, find the endpoint (DNS name) and port number for your DB Instance.

   * Navigate to **Databases** and click on **mydatabaseinstance.**
   * Under **Connectivity & security** section, copy and note the **endpoint** and **port**.

     **Example**:

     * **Endpoint :** mydatabaseinstance.c81x4bxxayay.us-east-1.rds.amazonaws.com
     * **Port :** 3306

       <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/016.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=ed2664143ed1443c719487f5105b45fb" alt="" width="1082" height="1048" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/016.png" />

2. Open MySQL Workbench. Click on the **MYSQL Connection plus icon IN your left** **side.**

   * Connection Name: Enter a sample name ***MyDatabseConnection***
   * Connection Method: Select **Standard (TCP/IP)**
   * Host Name: Enter your RDS endpoint : **mydatabaseinstance.c81x4bxxayay.us-east-1.rds.amazonaws.com**
   * Port: **3306**
   * Username: ***mydatabaseuser***
   * Password: Click on **Store in Vault** and enter password ***mydatabasepassword*** Click on the **OK** button.

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/017.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=da0e8eec408475c5393f11597065ec91" alt="" width="1222" height="268" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/017.png" />
   * Now click on the test connection button. It shows success connection message.

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/018.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=9e6e9bba605c506e0788ba2443fa1166" alt="" width="496" height="772" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/018.png" />
   * Now click on **OK** button and then click on the **OK** button in the main screen.

3. A database connection will be created in MySQL Workbench.

4. Wait for a few seconds to connect to your RDS Machine.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/019.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=357032dfe3978fa5504880a4583ae50e" alt="" width="522" height="192" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/019.png" />

5. If you see a similar screen as shown below then you have successfully connected to the RDS.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/020.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=d74de911b0504fdad2c8fe84a6785642" alt="" width="1222" height="640" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/020.png" />

6. Now copy the below MySQL command and paste it in the Query tab.

CREATE DATABASE StudentDB;

Use StudentDB;

CREATE TABLE students (

studentId INT AUTO\_INCREMENT,

studentName VARCHAR(50) NOT NULL,

Course VARCHAR(55),Semester VARCHAR(50) NOT NULL,PRIMARY KEY (studentId));

INSERT INTO students(studentName, Course, Semester) VALUES ('Paul', 'MBA', 'Second');

INSERT INTO students(studentName, Course, Semester) VALUES ('John', 'IT', 'Third');

INSERT INTO students(studentName, Course, Semester) VALUES ('Sebastian', 'Medicine', 'fifth');

SELECT \* FROM students;

7. MySQL Query Explanation:

* Create a Database StudentDB.

```
CREATE DATABASE StudentDB;
```

* Select the database

```
Use StudentDB;
```

* Create a table student with fields - student id, studnentName, Course and Semester.

```
CREATE TABLE students (studentId INT AUTO_INCREMENT, studentName VARCHAR(50) NOT NULL, Course VARCHAR(55),Semester VARCHAR(50) NOT NULL,PRIMARY KEY (studentId));
```

* Insert three values to the table.

```
INSERT INTO students(studentName, Course, Semester) VALUES ('Paul', 'MBA', 'Second');
```

* View the table data.

```
SELECT * FROM students;
```

8. Now Click on the **Execute** icon to start the execution and wait for a few minutes.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/021.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=4ed7f3796b911c1cbd300b61fb3a1b07" alt="" width="1222" height="664" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/021.png" />

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/022.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=971020146fb8c56571dfd0bc810c80e6" alt="" width="1222" height="221" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/022.png" />

9. Now You will be able to see the students table with these following values.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/023.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=bffe5ac8921408cff5e3c375a439076c" alt="" width="542" height="208" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/023.png" />

**Don't close the MySQL workbench window, keep it as minimized.**

#### Task 5: Create a Lambda function

1. Navigate to the **Services** menu at the top, then click on **Lambda** under the **Compute** section.
2. Click on **Create function**

   * Select **Author from Scratch**
   * Function Name : Enter ***MyRDSLambda***
   * Runtime : Select **the latest version of Python**

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/024.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=16f85498a851c5310d0db814a78f25f0" alt="" width="1222" height="513" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/024.png" />
   * Permissions :

     * Change default execution role : Select **Use an existing role**
   * Existing Role : Select ***task182\_role\_\<RANDOM\_NUMBER>.*** This role has already been created for you.

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/025.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=4d970b839df8cc3b51368ef5a44a843c" alt="" width="1222" height="578" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/025.png" />
3. Click on **Create Function** Button.
4. Once the Lambda Function is created successfully, it will look like the screenshot below:

<img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/026.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=08745b57754431a947470af5c9dc4aad" alt="" width="1222" height="524" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/026.png" />

5. Click on this [link](https://labresources.whizlabs.com/bfc92112b69418ca6b34e00b4e475e78/RDS_SQL_query.zip) to download the Lambda zip code
6. Click on the **Upload from** button and select the **.zip file** option

   * Click the **Upload** button and upload the zip file that we just downloaded and the click on the **Save** button.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/027.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=c0df045e00c2c3e8ccab0dd63c4f9515" alt="" width="964" height="332" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/027.png" />

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/028.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=343c630829eb4f936c5af0aa1696fea5" alt="" width="1222" height="552" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/028.png" />
7. Now in the code section, go to zip file and select **lambda\_function**.**py**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/K03DxSqV4ATZ2nJJ/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/029.png?fit=max&auto=format&n=K03DxSqV4ATZ2nJJ&q=85&s=78712f548ea9a50ba32eb9ebf5b7cd67" alt="" width="1222" height="485" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/029.png" />
8. In the code, go to line 5 and replace the **endpoint, username and password** in the python code with your values and click on the **Deploy** button. Wait till the deployment gets success.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/030.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=86f0dc7eddf473900568941197661f38" alt="" width="1222" height="411" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/030.png" />
9. **Lambda code explanation** :

   * The zip file contains pre installed python module used for performing MySQL queries in Python.
   * First we provide the RDS endpoint, username, password and database values.
   * Create a  connection to the RDS instance.
   * Perform MySQL query “SELECT \* FROM students” to fetch the table data.
   * Create a json value and print the table data in the lambda function.
10. Now click on the **Test** button and select **Create new test event**,

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/031.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=44eccde1e18884241ff3dd2f8b98682e" alt="" width="1222" height="464" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/031.png" />

    * Event name : Enter ***TestRDS***
    * Leave everything as default
    * Click the **Save** button.

      <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/032.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=fd98036595de72ea68d1f04666737184" alt="" width="1020" height="722" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/032.png" />
11. Now click on the **Invoke** button and you will be able to see the **students** table values in the Lambda output (JSON).

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/033.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=30d006eb0833b30e1984b5b2bacc39d2" alt="" width="1092" height="918" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/033.png" />
12. Scroll down the **Execution result** : (String)

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/034.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=479b43b62684c5e836f6303fcd5238af" alt="" width="1222" height="496" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/034.png" />
13. Go to **Function code**, inside the **lambda\_function**.**py** replace the existing code with the below:

    ```
    import json
    import pymysql

    RDS_endpoint = "myrdsinstance.cdegnvsebaim.us-east-1.rds.amazonaws.com"
    UserName = "mydatabaseuser"
    Password = "mydatabasepassword"
    DatabaseName = "StudentDB"

    connectionString = pymysql.connect(RDS_endpoint, user=UserName, passwd=Password, db=DatabaseName)

    def lambda_handler(event, context):
        header = []
        row_json = []
        table_json = {}
        pointer = connectionString.cursor()
        pointer.execute("INSERT INTO students(studentName, Course, Semester) VALUES ('Elizabeth', 'Art', 'first')")
        connectionString.commit()
        connectionString.close()
        return ("Insertion Success")
    ```
14. On line number 5, replace the **endpoint, username and password** in the python code with your values and click on the **Deploy** button.

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/035.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=77e2ecbfe64a54cc3a9c964a9bbf6644" alt="" width="1222" height="441" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/035.png" />
15. **Lambda code explanation** :

    * This code is used to insert a value into the **students** table.
    * First we provide the RDS endpoint, username, password and database values.
    * Create a  connection to the RDS instance.
    * Perform MySQL query “INSERT INTO students(studentName, Course, Semester) VALUES ('Elizabeth', 'Art', 'first')” to insert value into the table.
16. Click on the **Test** button and select **TestRDS** and click the **Invoke** button, you will get an insertion success message in lambda response.

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/036.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=35bb18f8e1956c2a5a72a2f751abd17d" alt="" width="770" height="270" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/036.png" />
17. Now navigate to the MySQL Workbench application and replace the existing query with the below.

    ```
    Use StudentDB;
               
    SELECT * FROM students;
    ```

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/037.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=fca5e261846a0c82bfcb4dfd3a7bfd15" alt="" width="956" height="320" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/037.png" />
18. Now Click on the  **Execute** icon to start the execution and wait for a few minutes.

> **Note** : Since the MySQL workbench was not active for a long time, it will take a few minutes to complete the execution. if you find difficulty then please close the connection and reconnect to RDS again from MySQL Workbench.

21. You will be able to see the new row that we just added to the table through the Lambda function.

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/038.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=8cd961ed6f843ebda91a43a3ae9e070c" alt="" width="544" height="232" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/038.png" />

> ##### DO you know?
>
> MySQL is a popular open-source relational database management system **(RDBMS)** that is widely used for managing structured data. MySQL in RDS refers to the MySQL database engine offered as a fully managed service within the **AWS ecosystem**. It allows you to run MySQL databases in the cloud without having to worry about the underlying infrastructure management tasks, such as **hardware provisioning, database setup, patching, backups, and automatic software updates.**

1. Once the lab steps are completed, please click on the **Validation** button on the left side panel.

#### Task 6: Delete AWS Resources

##### Delete Lambda Function

1. Make sure you are in the **US East (N. Virginia) us-east-1** Region.
2. Navigate to **Lambda** by clicking on the **Services** menu at the top, then click on **Lambda** in the **Compute** section.
3. Select the lambda function **MyRDSLambda** and click **Actions** and click **Delete** button.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/040.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=77a6318863b9618d8b9bad9b246ca005" alt="" width="1222" height="162" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/040.png" />
4. Enter the **confirm** in the required field and click the **Delete** button to confirm deletion.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/041.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=fd44bf462d6d14edd77825667899bc47" alt="" width="1222" height="472" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/041.png" />

##### Delete RDS Instance

1. Make sure you are in the **US East (N. Virginia) us-east-1** Region.
2. Navigate to **RDS** by clicking on the **Services** menu available under the **Database** section.
3. Select **Databases** from the left side menu.
4. Select the RDS instance **mydatabaseinstance** and click on **Actions** and select **Delete**.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/042.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=2baf7ee8bee6f2830dc144a376e98166" alt="" width="1222" height="213" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/042.png" />
5. Confirmation Prompt :

   * Uncheck the option : **Create the final snapshot**
   * Check the **I Acknowledge** option
   * In textbox : Enter ***delete me***
   * Now click on the **Delete** button.

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/OGWG_Fy9il7e0mi5/images/labs/how-to-query-into-rds-mysql-using-aws-lambda/043.png?fit=max&auto=format&n=OGWG_Fy9il7e0mi5&q=85&s=839aa29b5e25e1b755cdb4451180231d" alt="" width="1158" height="1166" data-path="images/labs/how-to-query-into-rds-mysql-using-aws-lambda/043.png" />
6. Now click on the refresh button and you will be able to see that the status of the RDS instance is **deleting**. It will take 10 to 20 Minutes to delete the instance.

### Completion and Conclusion

1. You have successfully logged into AWS management console.
2. You have successfully created a security group for RDS instance.
3. You have successfully created a RDS Instance.
4. You have successfully connected to RDS using MySQL workbench.
5. You have successfully created a Lambda function.

### 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:

* **Create an AWS Lambda Function** — Check whether a Lambda Function is created or not
* **Check whether RDS database Engine type is mysql** — Check whether allowed database type mysql is deployed or not


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