aws rds mysql 连接,可以AWS LAMBDA连接到RDS MySQL数据库和更新数据库?

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();

};

解决方案

Yes. You can access a MySql RDS database from AWS Lambda.

You can use node-mysql library.

However, there is a big caveat that goes with it.

AWS Lambda does not (currently) have access to private subnets inside a VPC. So in order for AWS Lambda to access your RDS database, it must be publicly accessible, which could be a security risk for you.

Update (2015-10-30): AWS Lambda announced upcoming VPC support (as of re:Invent 2015), so this won't be an issue for much longer.

Update (2015-11-17): AWS Lambda still does not have VPC support.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值