Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

876
Visualizações
AWS Lambda functions NodeJs for postgreSQL - timeout error

I am new to AWS. I am trying to connect to AWS RDS postgreSQL instance using Lambda functions. I followed the aws documentation. But it uses python for Lambda functions. Below is my code.

'use strict';
var pg = require('pg');
exports.handler = function (event, context) {
    var dbConfig = {
        username: '<username>',
        password: '<password>',
        database: '<database>',
        host: '<db-endpoint>',
    };
    var client = new pg.Client(dbConfig);
    try {
        client.connect();
        context.callbackWaitsForEmptyEventLoop = false;
        client.end();
    }
    catch (err) {
        console.log(err);
        client.end();
    }
};

I am getting timeout error as below

START RequestId: 368e619e-ed9d-4241-93a5-764ee01aa847 Version: $LATEST
2020-06-15T16:28:18.911Z    368e619e-ed9d-4241-93a5-764ee01aa847    INFO    connected
END RequestId: 368e619e-ed9d-4241-93a5-764ee01aa847
REPORT RequestId: 368e619e-ed9d-4241-93a5-764ee01aa847  Duration: 20020.16 ms   Billed Duration: 20000 ms   Memory Size: 128 MB Max Memory Used: 70 MB  Init Duration: 150.01 ms    
2020-06-15T16:28:38.901Z 368e619e-ed9d-4241-93a5-764ee01aa847 Task timed out after 20.02 seconds

Please advise on the error.

I have few other questions to ensure if my code is correct

  • I gave db instance endpoint url for db-endpoint. is that right? or if not what should i use there?

  • is there any proper documentation available, for the beginners like me, about Lambda functions with nodejs to connect postgres on RDS?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You're not returning anything from the lambda. So the request keeps hanging without a response until it times out.

Use the third argument callback supplied to the handler to respond or return a Promise.

'use strict';
var pg = require('pg');
exports.handler = function (event, context,callback) {
    var dbConfig = {
        username: '<username>',
        password: '<password>',
        database: '<database>',
        host: '<db-endpoint>',
    };
    var client = new pg.Client(dbConfig);
    try {
        client.connect();
        context.callbackWaitsForEmptyEventLoop = false;
        client.end();

        //send the response
        callback(null,"Some Response")
    }
    catch (err) {
        console.log(err);
        client.end();
        callback(err)
    }
};

AWS example : AWS Lambda NodeJS Connect to RDS Postgres Database

You can read the official js docs with all methods and properties here : https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/RDS.html

Hope this helps !

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda