Skip to content

Instantly share code, notes, and snippets.

@samueleastdev
Last active March 25, 2019 11:09
Show Gist options
  • Select an option

  • Save samueleastdev/c29591cd570048b901f52cf1b5be3c69 to your computer and use it in GitHub Desktop.

Select an option

Save samueleastdev/c29591cd570048b901f52cf1b5be3c69 to your computer and use it in GitHub Desktop.
CRUD Lambda RDS Template
'use strict';
var mysql = require('mysql');
exports.handler = (event, context, callback) => {
var connection = mysql.createConnection({
host : 'rds.amazonaws.com',
database : 'database',
user : 'user',
password : 'password',
port : '3306'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
switch (event.method) {
case 'DELETE':
callback(null, {
statusCode: '400',
body: "No Delete method",
headers: {
'Content-Type': 'application/json',
},
});
break;
case 'GET':
callback(null, {
statusCode: '400',
body: "No GET method",
headers: {
'Content-Type': 'application/json',
},
});
break;
case 'POST':
var Id = event.query.code;
connection.query('SELECT * FROM table WHERE code = "' + Id + '"', function(err, rows, fields) {
callback(null, {
event: event,
statusCode: err ? '400' : '200',
body: err ? err.message : rows,
headers: {
'Content-Type': 'application/json',
},
});
});
break;
case 'PUT':
callback(null, {
statusCode: '400',
body: "No PUT method",
headers: {
'Content-Type': 'application/json',
},
});
break;
default:
callback(null, {
event: event,
statusCode: '400',
body: "No PUT method",
headers: {
'Content-Type': 'application/json',
},
});
}
connection.end();
};
@codebykyle
Copy link

Use this if you love SQL injection

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