Last active
July 9, 2019 21:50
-
-
Save MrHassanMurtaza/f83ae5b9c6e13284a8acb140a143e004 to your computer and use it in GitHub Desktop.
Connect Lambda to RDS MySql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mysql = require('mysql'); | |
var config = require('./config.json'); | |
exports.handler = function(event, context, callback) { | |
var pool = mysql.createPool({ | |
host : config.dbhost, | |
user : config.dbuser, | |
password : config.dbpassword, | |
database : config.dbname, | |
queueLimit : 0, // unlimited queueing | |
connectionLimit : 10, | |
multipleStatements : true, | |
connectTimeout : 60 * 60 * 1000, | |
acquireTimeout : 60 * 60 * 1000, | |
timeout : 60 * 60 * 1000, | |
}); | |
// console.log(pool) | |
pool.getConnection(function(err, connection) { | |
context.callbackWaitsForEmptyEventLoop = false; | |
if (err) { console.log('===============>', err) } | |
// Use the connection | |
connection.query('SELECT * from Table', function (error, results, fields) { | |
// And done with the connection. | |
connection.release(); | |
// Handle error after the release. | |
if (error) throw error; | |
else console.log(results); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Config.json:
{
"dbhost" : "",
"dbname" : "",
"dbuser" :"",
"dbpassword" :""
}