0
votes

I've created a Lambda that connects inconsistently to a PostgreSQL database in RDS.

When it works, it connects and completes the query in a small amount of time. Less than 100ms, usually only 20ms. I can execute this lambda ten or twenty times and it will work fine. Eventually though, it gets stuck trying to connect to the database.

The connection will time out eventually. I have set the timeout to be thirty seconds. Extending it doesn't seem to make a difference.

When it does get stuck connecting, I can sometimes try it half an hour later or so and it will be fine. Sometimes it will still fail, even after a day.

It usually works for a while when I upload a new zip. Sometimes, when it doesn't connect to the database immediately after the upload of a new zip, I can get it to work by editing the lambda in the AWS Lambda console and clicking Deploy from there.

Has anyone seen this behaviour before? What am I doing wrong? I'm new to node and Lambdas. I created a lambda in C# as well and get the same behaviour.

The database instance is correctly provisioned and has only a small number of connections.

Here is a sample of the code. I don't use the dbConfig as I have stored them as environment variables. The zip uploaded to Lambda contains this file and the node_modules folder for 'pg' and its dependancies.

'use strict';
var pg = require('pg');

exports.handler = async (event, context, callback) => {
  var dbConfig = {
    username: '<username>',
    password: '<password>',
    database: '<database>',
    host: '<database_endpoint>',
    };
  var client = new pg.Client(dbConfig);
    
  console.log('Waiting to connect'); 
  await client.connect();
  console.log('Connected'); 
  
  console.log('Querying the database');
  var res = await client.query('SELECT COUNT(*) FROM books;'); // 'books' is a table in the database
  console.log(res);
  
  await client.end();
  console.log('Connection closed');
};

Using pg.Pool instead of pg.Client hasn't helped.

Moving the connection code outside of the handler only seems to work with callbacks. Callbacks don't seem to work well with Lambdas anymore.

1
Is lambda and PostgeSQL RDS is in the same account / VPC ?Nikhil B
This can also occur if you configure the Lambda function to run in VPC and on multiple subnets and one of the subnets has an incorrectly configured route table. aws.amazon.com/premiumsupport/knowledge-center/…Nikhil B

1 Answers

0
votes

Nikhil B's comment solved this. I removed a subnet with a bad route table and the connection stabilised.

No more timeouts waiting to connect to the db.