Step 2: Create Lambda and API Gateway (Nodejs) — AWS Serverless to RDS MySQL
Create Lambda
NodeJS Code
The online editor of the lambda cannot use the “npm install” and therefore we need to write code in our computer and then upload to the lambda.
Step 1: Create package.json
npm init
The package.json for your reference.
{
"name": "test2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Step 2: Install the “mysql” library for connecting the MySQL
npm install --save mysql
Step 3: Create Database in index.js
const mysql = require('mysql');const con = mysql.createConnection({
host : process.env.RDS_HOSTNAME,
user : process.env.RDS_USERNAME,
password : process.env.RDS_PASSWORD,
port : process.env.RDS_PORT
});exports.handler = async (event) => {
con.query("CREATE DATABASE mysqllab", function (err, result) {
if (err) throw err;
console.log("Database created");
});
return "Database Created"
};
Step 4: Zip all the node_modules, index.js and package.json
The RDS_HOSTNAME got from the RDS details section.
Change the existing role from “AWSLambdaEdgeExecutionRole” to “AWSLambdaVPCAccessExecutionRole”
Change the RDS MySQL Security Group
You can find your security group in the MySQL details section.
“sg-fa8db185” can from the Network section in the lambda.
Create Table in the Database
Using the same Lambda to create table — MESSAGE and change the following source code to create the table in online editor.
const mysql = require('mysql');const con = mysql.createConnection({
host : process.env.RDS_HOSTNAME,
user : process.env.RDS_USERNAME,
password : process.env.RDS_PASSWORD,
port : process.env.RDS_PORT,
database : process.env.RDS_DATABASE
});exports.handler = async (event) => {
const sql = "CREATE TABLE MESSAGE (message VARCHAR(255))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
return "Table Created"
};
Insert Record into Table “MESSAGE”
Change to the following source code to insert the record.
const mysql = require('mysql');const con = mysql.createConnection({
host : process.env.RDS_HOSTNAME,
user : process.env.RDS_USERNAME,
password : process.env.RDS_PASSWORD,
port : process.env.RDS_PORT,
database : process.env.RDS_DATABASE
});exports.handler = (event, context, callback) => {
// allows for using callbacks as finish/error-handlers
context.callbackWaitsForEmptyEventLoop = false;
const sql = "INSERT INTO MESSAGE (message) VALUES ('I am MySQL')";
con.query(sql, (err, res) => {
if (err) {
throw err
}
callback(null, '1 records inserted.');
})
};
Query the Record
Change to the following source code to query the record.
const mysql = require('mysql');const con = mysql.createConnection({
host : process.env.RDS_HOSTNAME,
user : process.env.RDS_USERNAME,
password : process.env.RDS_PASSWORD,
port : process.env.RDS_PORT,
database : process.env.RDS_DATABASE
});exports.handler = (event, context, callback) => {
// allows for using callbacks as finish/error-handlers
context.callbackWaitsForEmptyEventLoop = false;
const sql = "select * from MESSAGE";
con.query(sql, function (err, result) {
if (err) throw err;
callback(null, result)
});
};
Create API Gateway
You can give me the response for support if you completed the lab or ask me questions when you face any difficulty.
Review how to create RDS MySQL in the AWS