简体   繁体   English

AWS Lambda/Nodejs 没有执行 mysql connection.query

[英]AWS Lambda/Nodejs is not executing mysql connection.query

I have a simple Lambda function to connect to MYSQL, but the connection.query is not executing.我有一个简单的 Lambda 函数来连接到 MYSQL,但是connection.query没有执行。 Lambda has AWSLambdaVPCAccessExecutionRole access and it is part of the same VPC where RDS is deployed. Lambda 具有AWSLambdaVPCAccessExecutionRole访问权限,并且它是部署 RDS 的同一 VPC 的一部分。

RDS security group is added to Lambda's configuration. RDS 安全组已添加到 Lambda 的配置中。 log1 is the output that I always get. log1 是我总是得到的输出。 One time i got Log2.有一次我得到了Log2。 I am not sure what is going on.我不确定发生了什么。

    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日志1

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 RESTART RequestId:04c2d758-ac40-474f-ac55-045ccea8a9ff 版本:$LATEST 2020-11-08T02:02:58.976Z 04c2d758-ac40-474f-ac55-045ccea8a9ff 信息 3 END RequestId:04c2d758-ac40-cceaa-4874fPORT RequestId:04c2d758-ac40-474f-ac55-045ccea8a9ff 持续时间:5.75 毫秒计费持续时间:100 毫秒内存大小:128 MB 使用的最大内存:20 MB

Log2日志2

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)"," START RequestId: abeb6a6d-7480-4acc-91e2-5971e6fe74fe 版本: $LATEST 2020-11-08T02:02:43.293Z abeb6a6d-7480-4acc-91e2-5971e6fe74fe INFO 3 2020-11-08Z02:02:43.293Z abeb6a6d-7480-4acc-91e2-5971e6fe74fe 信息 3 2020-11-08Z02:02:43.284a a4be769。 -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)","在 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在 Connection.query (/var/task/node_modules/mysql/lib/Connection.js:198:25)"," 在 Runtime.exports.handler (/var/task/index.js:18:16)","在 Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]} [错误] [1604800963301] LAMBDA_RUNTIME 未能发布处理程序成功响应。Http 响应代码:403。END RequestId:abeb6a6d-7480-4acc- 91e2-5971e6fe74fe 报告请求 ID:abeb6a6d-7480-4acc-91e2-5971e6fe74fe 持续时间:29.47 毫秒计费持续时间:100 毫秒内存大小:128 MB 使用的最大内存:20 MB
RequestId: abeb6a6d-7480-4acc-91e2-5971e6fe74fe Error: Runtime exited with error: exit status 129 Runtime.ExitError RequestId:abeb6a6d-7480-4acc-91e2-5971e6fe74fe 错误:运行时退出错误:退出状态 129 Runtime.ExitError

Your handle is defined to be async and you didn't await to the promise.您的句柄被定义为async并且您没有await承诺。 Possible solutions:可能的解决方案:

  1. Add await call to your promise (ie await connection.query(...).promise() )将 await 调用添加到您的承诺中(即await connection.query(...).promise()
  2. Change the handler's signature to be non-async (ie exports.handler = (event,context,callback) => {...} ) and then it will wait to the callback.将处理程序的签名更改为非异步(即exports.handler = (event,context,callback) => {...} ),然后它将等待回调。
  3. (Ugly workaround) Add context.callbackWaitsForEmptyEventLoop = true; (丑陋的解决方法)添加context.callbackWaitsForEmptyEventLoop = true; , which tells AWS to wait for an empty event loop. ,它告诉 AWS 等待一个空的事件循环。

Disclosure: I work for Lumigo, a company that helps you to debug serverless applications.披露:我在 Lumigo 工作,这是一家帮助您调试无服务器应用程序的公司。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM