Srinath
Srinath

Reputation: 295

AWS Lambda stops execution in the middle of the code

I am trying to trigger csv file upload in s3 and insert the data from the file to database using lambda.

Most of the times code executes successfully if i run the code back to back in couple of seconds gap.

But sometimes the problem i face is the code stops execution at console console.log('about to get the data'); and ignore rest of the code and sometimes mysql connection gets time out.

I can find that the problem occurs only when i test the lambda code with more than 20 seconds of gap. So, i guess this is a cold start problem.

I don't want to miss even a single s3 trigger. So, i need help to find flaw in my code that is causing this problem.

   const AWS = require('aws-sdk');
    const s3 = new AWS.S3({region: 'ap-south-1', apiVersion: '2006-03-01'});
    var mysql= require('mysql');

    var conn = mysql.createPool({
        connectionLimit: 50,
        host: 'HOST',
        user: 'USER',
        password: 'PASSWORD',
        database: 'DATABASE'
      })

    async function mainfunc (event, context, callback) {
        console.log("Incoming Event: ", JSON.stringify(event));
        const bucket = event.Records[0].s3.bucket.name;
        const filename = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));

        const params = {
            Bucket: bucket,  
            Key:  filename
        };


        console.log('about to get the data'); //Code stops here some times

          return await getresult(params);


    };

    async function getresult(params){
        var result =  await s3.getObject(params).promise();


        var recordList = result.Body.toString('utf8').split(/\r?\n/).filter(element=>{
            return element.length> 5;
          })
          recordList.shift()
          var jsonValues = [];
          var jsonKeys = result.Body.toString('utf8').split(/\r?\n/)[0]

          recordList.forEach((element) => {
            element = element.replace(/"{2,}/g,'"').replace(/, /g,"--").replace(/"{/, "{").replace(/}"/, "}").replace(/,/g, ';').replace(/--/g,', ').split(';');
            jsonValues.push(element)
          });

          var lresult = await query(jsonKeys, jsonValues);        
          return lresult;

    }

    async function query(jsonKeys, jsonValues){
      var qresult = await conn.getConnection(function(err, connection) {
        if (err){
          console.log(err,'------------------------------------');// Sometimes i get Sql Connection timeout error here
        } else {
        console.log("Connected!");
        var sql = "INSERT INTO reports ("+jsonKeys+") VALUES ?";
        connection.query(sql, [jsonValues], function (err, result) {
          if (err){
            console.log(err);
            connection.release()
            return err;
          } else {
          console.log("1 record inserted");
          console.log(result);
          connection.release()
          return result;
          }
        });
      }
      })

    }



      exports.handler = mainfunc

Upvotes: 2

Views: 961

Answers (1)

Srinath
Srinath

Reputation: 295

I have solved the issue by using promise in the "query" function

function query(jsonKeys, jsonValues){
      return new Promise(function(resolve, reject) {
      conn.getConnection(function (err, connection) {
        if (err) {
          console.log(err, '------------------------------------');
        }
        else {
          console.log("Connected!");
          var sql = "INSERT INTO principal_reports (" + jsonKeys + ") VALUES ?";
          connection.query(sql, [jsonValues], function (err, result) {
            if (err) {
              console.log(err);
              connection.release();
              reject(err)
            }
            else {
              console.log("1 record inserted");
              console.log(result);
              connection.release();
              resolve(result)
            }
          });
        }
      })
    })
    }

and changed the code

var lresult = await query(jsonKeys, jsonValues);

to

var lresult = await query(jsonKeys, jsonValues).then(data =>{
            return data;
          }).catch(error =>{
            return error;
          });

Upvotes: 1

Related Questions