Created
December 22, 2020 19:10
-
-
Save ashutoshpw/68cdc2a5e22599db5693ed13a1d95d92 to your computer and use it in GitHub Desktop.
ConnectToMySQLFromAWSLambda
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
'use strict'; | |
const mysql = require('mysql'); // require mysql | |
// If 'client' variable doesn't exist | |
if (typeof client === 'undefined') { | |
// Connect to the MySQL database | |
var client = mysql.createConnection({ | |
// your connection info | |
}); | |
client.connect() | |
} | |
module.exports.handler = (event, context, callback) => { | |
// This will allow us to freeze open connections to a database | |
context.callbackWaitsForEmptyEventLoop = false; | |
client.query('SELECT * FROM `table_name`', function (error, results) { | |
callback(null, results) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment