I am trying to connect AWS Lambda function to RDS mysql database.
I just wanted to update the database from my lambda function. Is it possible to access RDS by specifiying IAM Role and access Policy?.
I can connect to mysql databse using mysql client.but when i try on lambda i can't do that. here is my code.
console.log('Loading function');
var doc = require('dynamodb-doc');
var dynamo = new doc.DynamoDB();
var mysql = require('mysql');
exports.handler = function(event, context) {
//console.log('Received event:', JSON.stringify(event, null, 2));
var operation = event.operation;
delete event.operation;
switch (operation) {
case 'create':
var conn = mysql.createConnection({
host: 'lamdatest.********.rds.amazonaws.com', // RDS endpoint
user: 'user', // MySQL username
password: 'password', // MySQL password
database: 'rdslamda'
});
conn.connect();
console.log("connecting...");
conn.query('INSERT INTO login (name,password) VALUES("use6","password6")', function(err, info) {
console.log("insert: " + info.msg + " /err: " + err);
});
console.log("insert values in to database");
break;
case 'read':
dynamo.getItem(event, context.done());
break;
default:
context.fail(new Error('Unrecognized operation "' + operation + '"'));
}
context.succeed();
};
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…