2

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
3
  • 1
    close you conn by con.close()/qresult.close() after loading data into database or at the end of your code Commented Dec 30, 2019 at 6:06
  • I have added "conn.close();" before "return result;". I am getting error "conn.close is not a function" Commented Dec 30, 2019 at 6:14
  • dp connection.close() do apply close() on var on which you are creating connectionstring(get connection) Commented Dec 30, 2019 at 6:21

1 Answer 1

1

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;
          });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.