Ela Buwa
Ela Buwa

Reputation: 1704

Nodejs lambda function timing out

Just trying out Lambda and nodejs. I have a timeout error that happens every few times.

My nodejs code is below.

var mysql = require('mysql');

var pool = mysql.createPool({
  connectionLimit: 100,
  host     : 'hostname',
  user     : 'username',
  password : 'password',
  database : 'database',
  port : 3306
});


exports.handler = async (event, context, callback) => {
    let request = JSON.parse(event.body);
    let sId = request.sid;
    let questionnaireId = request.questionnaireId;

    pool.getConnection((error, connection) => {
        if (error) throw error;
        let sql = "INSERT INTO answers (qId, sId,) VALUES(" + questionnaireId + ", " + sid + ")";
        connection.query(sql, function(err, result){
            if(err) throw err;
            console.log("Successfull Insert")
            connection.release();
        });

    });

    const response = {
        statusCode: 200,
        headers: {
                    'Access-Control-Allow-Origin': '*',
                    'Access-Control-Allow-Credentials': true
                },
        body: JSON.stringify({message : 'success'}),
    };

    return response;
};

Cloudwatch shows the below message.

"errorMessage": "connect ETIMEDOUT", "code": "ETIMEDOUT",

Thanks in advance.

Upvotes: 0

Views: 1995

Answers (2)

Zolbayar
Zolbayar

Reputation: 936

Replace

return response;

with this:

callback(null, response)

Reference: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

Upvotes: 0

Marcos Luis Delgado
Marcos Luis Delgado

Reputation: 1439

Try removing callback argument from your handler. You are returning a promise from your function, so you don't need it.

Defining callback as an argument probably makes aws think you are gonna call it when your function finishes executing, and as you don't it produces a timeout error.

Upvotes: 1

Related Questions