2
votes

I am working on an application where i want to connect to postgresql database from AWS lambda.I have setup the database with Publicly Accessible property set as true.Inbound and outbound policies are enter image description here

enter image description here

I am able to access the database through pgAdmin client but not able to do so through aws lambda.I have also associated AWSRDSFULLACCESS policy to lambda role and added vpc and subnet and security groups information to the advanced settings section.

I am getting following timeout error errorMessage": "2017-04-15T11:26:05.163Z 4ac2cf66-21ce-11e7-a6da-a7d26945c336 Task timed out after 9.00 seconds"

Node.js code I am using to connect to RDS is

var pg = require("pg");
exports.handler = (event, context, callback) => {
    // TODO implement
    const connectionStr = "pg://username:password@hostendpoint:5432/database name";
  var client = new pg.Client(connectionStr);
  client.connect(function(err){
    if(err) {
      callback(err)
    }
    callback(null, 'Connection established');
  });
};

How can I access the RDS from AWS Lambda?

1
Run the code locally to weed out the connection issue first.mootmoot

1 Answers

0
votes

Did you try logging something from database to confirm it doesn't access it?

To me it seems like the problem is that you don't end the connection. If you have open database connection, callback doesn't terminate it, it keeps running until it times out.

Try using client.end() to terminate the connection.