0
votes

I have a simple lambda function to connect to mysql, but the connection.query is not executing. lambda has AWSLambdaVPCAccessExecutionRole access and it is part of the same vpc where rds is deployed. rds security group is added to lambda's configuration. log1 is the output that i always get. One time i got Log2. I am not sure what is going on. Can anyone pleas help?

-Ann

    var mysql      = require('mysql');
    var connection = mysql.createConnection({
      host     : 'xxxxxxxx.us-east-1.rds.amazonaws.com',
      user     : 'admin',
      password : 'pass',
      database : 'dbname'
    });
    console.log('CONNECTION ' + connection);
    connection.connect();
    console.log('2 ');  
    exports.handler = async (event,context,callback) => {
      console.log('3 ');
      context.callbackWaitsForEmptyEventLoop = false;
        connection.query('SELECT emp_num from tag where emp_id=1', function (error, results, fields) {
          console.log('2 ');
          if (error) throw error;
          else
            callback(null, results)
          //console.log('The solution is: ', results[0].tag_num);
        });
        connection.end();
    };

Log1

START RequestId: 04c2d758-ac40-474f-ac55-045ccea8a9ff Version: $LATEST 2020-11-08T02:02:58.976Z 04c2d758-ac40-474f-ac55-045ccea8a9ff INFO 3 END RequestId: 04c2d758-ac40-474f-ac55-045ccea8a9ff REPORT RequestId: 04c2d758-ac40-474f-ac55-045ccea8a9ff Duration: 5.75 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 20 MB

Log2

START RequestId: abeb6a6d-7480-4acc-91e2-5971e6fe74fe Version: $LATEST 2020-11-08T02:02:43.293Z abeb6a6d-7480-4acc-91e2-5971e6fe74fe INFO 3 2020-11-08T02:02:43.294Z abeb6a6d-7480-4acc-91e2-5971e6fe74fe INFO 2 2020-11-08T02:02:43.294Z abeb6a6d-7480-4acc-91e2-5971e6fe74fe ERROR Uncaught Exception {"errorType":"Error","errorMessage":"Cannot enqueue Query after invoking quit.","code":"PROTOCOL_ENQUEUE_AFTER_QUIT","fatal":false,"stack":["Error: Cannot enqueue Query after invoking quit."," at Protocol._validateEnqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:215:16)"," at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:138:13)"," at Connection.query (/var/task/node_modules/mysql/lib/Connection.js:198:25)"," at Runtime.exports.handler (/var/task/index.js:18:16)"," at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]} [ERROR] [1604800963301] LAMBDA_RUNTIME Failed to post handler success response. Http response code: 403. END RequestId: abeb6a6d-7480-4acc-91e2-5971e6fe74fe REPORT RequestId: abeb6a6d-7480-4acc-91e2-5971e6fe74fe Duration: 29.47 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 20 MB
RequestId: abeb6a6d-7480-4acc-91e2-5971e6fe74fe Error: Runtime exited with error: exit status 129 Runtime.ExitError

1

1 Answers

0
votes

Your handle is defined to be async and you didn't await to the promise. Possible solutions:

  1. Add await call to your promise (i.e. await connection.query(...).promise())
  2. Change the handler's signature to be non-async (i.e. exports.handler = (event,context,callback) => {...}) and then it will wait to the callback.
  3. (Ugly workaround) Add context.callbackWaitsForEmptyEventLoop = true;, which tells AWS to wait for an empty event loop.

Disclosure: I work for Lumigo, a company that helps you to debug serverless applications.