0

I'm making Lambdas with node js and mysql connection to an RDS, the thing is the first time I trigger the Lambda function I get the error "Process exited before completing request", then it works fine.

Here is my code:

const mysql = require('mysql');

module.exports.selectAll = (event, context, callback) => {
    
    context.callbackWaitsForEmptyEventLoop = false;
    
    //Response
    let response = {
        status: 200,
        message: '',  
        data: [], 
    }
    
    //Body request
    const {user} = event
    
    const connection = mysql.createConnection({
        host: process.env.host_DB,
        user: process.env.user_DB,
        password: process.env.password_DB,
        database: process.env.database_DB
    
    });

    connection.open(); 

    connection.query(`call test_db.getAll('${user != undefined? user : 0}')`,
    function(err, results, fields) {
        if(err){
            console.log(err);
            response.status = 500; 
            response.message = err.message; 
            response.data = []; 
        }else{
            response.data = results[0]; 
            response.message = 'Success'; 
        }
        
        connection.end();
        callback(null, response);
    })
};

Any idea of what is causing it?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • If it only fails on the first try, it could be related to a cold start, when the [execution context](https://docs.aws.amazon.com/lambda/latest/dg/running-lambda-code.html?shortFooter=true) is first set up. You could try to increase the timeout... – Maurice Dec 26 '18 at 18:43
  • Maybe [this](https://stackoverflow.com/questions/31627950/aws-lambda-process-exited-before-completing-request) question is relevant as well, it could be related to memory consumption. – Maurice Dec 26 '18 at 18:49
  • Sprinkle some console.log lines into your code and narrow down on the specific offending line. – The-Big-K Dec 26 '18 at 23:20
  • I increase it to one minute and this still happens, I will try the console.log lines, thanks! – Andrea De Alba Dec 27 '18 at 19:15

0 Answers0