可以AWS LAMBDA连接到RDS MySQL数据库和更新数据库? [英] can AWS Lambda connect to RDS mySQL database and update the database?

查看:1878
本文介绍了可以AWS LAMBDA连接到RDS MySQL数据库和更新数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想AWS LAMBDA功能连接到RDS MySQL数据库。
我只是想更新从我的lambda函数的数据库。是否有可能通过specifiying IAM角色和访问策略?
访问RDS 我可以用mysql client.but当我尝试对拉姆达我不能这样做,连接到MySQL DATABSE。这里是我的code。

 的console.log('加载功能');
VAR DOC =需要('dynamodb-DOC);
VAR发电机=新doc.DynamoDB();
VAR的mysql =需要('MySQL的);
exports.handler =函数(事件上下文){
//console.log('Received事件:,JSON.stringify(事件,空,2));
变种运行= event.operation;
删除event.operation;
开关(操作){
案创造:
VAR康恩= mysql.createConnection({
主持人:。lamdatest ******** rds.amazonaws.com',// RDS端点
用户:用户,// MySQL用户名
密码:'密码',// MySQL的密码
数据库:'rdslamda
});
conn.connect();
的console.log(连接...);
conn.query(INSERT INTO登录(名,密码)VALUES(use6,password6)',函数(ERR,资讯){
        的console.log(插入+ info.msg +/错误:+ ERR);
    });
的console.log(在数据库插入值);
打破;
案读:
dynamo.getItem(事件,context.done());
打破;

默认:
context.fail(新的错误(无法识别的操作+操作+''));

}
context.succeed();
};
 

解决方案

是的。您可以从AWS LAMBDA访问MySQL的RDS数据库。

您可以使用节点的MySQL 库。

然而,与它去一个重要的提醒。

AWS LAMBDA不(目前)获得内部的VPC专用子网。因此,为了使AWS LAMBDA访问您的RDS数据库,它必须是可公开访问的,这可能是为你带来安全隐患。

更新(二○一五年十月三十○号)的:AWS LAMBDA宣布即将推出的VPC支持(<一href="https://aws.amazon.com/it/blogs/aws/aws-lambda-update-python-vpc-increased-function-duration-scheduling-and-more/"相对=nofollow>为重:创造2015年),因此这将不会持续太久是一个问题。

更新(2015年11月17日)的:AWS LAMBDA还没有VPC支持

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.

这篇关于可以AWS LAMBDA连接到RDS MySQL数据库和更新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆