Skip to content

Instantly share code, notes, and snippets.

@MrHassanMurtaza
Last active July 9, 2019 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrHassanMurtaza/f83ae5b9c6e13284a8acb140a143e004 to your computer and use it in GitHub Desktop.
Save MrHassanMurtaza/f83ae5b9c6e13284a8acb140a143e004 to your computer and use it in GitHub Desktop.
Connect Lambda to RDS MySql
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);
});
});
}
@MrHassanMurtaza
Copy link
Author

Config.json:
{
"dbhost" : "",
"dbname" : "",
"dbuser" :"",
"dbpassword" :""
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment