Home:ALL Converter>can AWS Lambda connect to RDS mySQL database and update the database?

can AWS Lambda connect to RDS mySQL database and update the database?

Ask Time:2015-08-04T20:53:35         Author:ARUNBALAN NV

Json Formatter

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

Author:ARUNBALAN NV,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31809890/can-aws-lambda-connect-to-rds-mysql-database-and-update-the-database
Matt Houser :

Yes. You can access a MySql RDS database from AWS Lambda.\n\nYou can use node-mysql library.\n\n\nLink: https://github.com/felixge/node-mysql/\n\n\nHowever, there is a big caveat that goes with it.\n\nAWS 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.\n\nUpdate (2015-10-30): AWS Lambda announced upcoming VPC support (as of re:Invent 2015), so this won't be an issue for much longer.\n\nUpdate (2015-11-17): AWS Lambda still does not have VPC support.\n\nUpdate (2016-02-11): AWS Lambda can now access VPC resources:\n\nhttps://aws.amazon.com/blogs/aws/new-access-resources-in-a-vpc-from-your-lambda-functions/\n\nTo achieve this functionality, your Lambda function will actually execute inside your VPC in a subnet. Some caveats come with this functionality:\n\n\nThe VPC subnet needs enough free IP addresses to handle Lambda's scaling\nIf your Lambda function needs internet access, then it's designated VPC subnet will need an Internet Gateway or NAT\n",
2015-08-04T14:01:22
Pallav :

try this tutorial:\nhttp://docs.aws.amazon.com/lambda/latest/dg/vpc-rds.html \n\nIn this tutorial, you do the following:\n\nLaunch an Amazon RDS MySQL database engine instance in your default Amazon VPC.\n\nIn the MySQL instance, you create a database (ExampleDB) with a sample table (Employee) in it.\n\nCreate a Lambda function to access the ExampleDB database, create a table (Employee), add a few records, and retrieve the records from the table.\n\nInvoke the Lambda function manually and verify the query results. ",
2017-03-12T10:51:00
Jordan :

Since Lambda uses Node.js, Java and Python as a backend programming/scripting language, you can definitely use it to connect to RDS. (Link)\n\nFinally, This is the documentation on specifying IAM Roles when connecting to RDS. (See image below):\n\n",
2015-08-04T13:09:12
yy