简体   繁体   English

AWS Lambda + Nodejs + MySQL

[英]AWS Lambda + Nodejs + MySQL

I am writing a simple lambda code to insert some values to mysql table (rds) but i have no clue why it is not working.我正在编写一个简单的 lambda 代码来向 mysql 表 (rds) 插入一些值,但我不知道它为什么不起作用。 After reperted tries sometimes it works and subsequent tries again stop working在重新尝试后有时它会起作用,随后的尝试再次停止工作

everytime it fails (does not insert to table) i get the below response每次失败(不插入到表中)我得到以下响应

Response:
null

Request ID:
"6814c4b8-65c5-4401-b7c1-ae06426f186c"

Function Logs:
START RequestId: 6814c4b8-65c5-4401-b7c1-ae06426f186c Version: $LATEST
END RequestId: 6814c4b8-65c5-4401-b7c1-ae06426f186c
REPORT RequestId: 6814c4b8-65c5-4401-b7c1-ae06426f186c  Duration: 317.80 ms Billed Duration: 400 ms     Memory Size: 128 MB Max Memory Used: 75 MB  

My Code to insert data or query data is as simple as我的插入数据或查询数据的代码很简单

const mysql = require('mysql');
const con = mysql.createConnection({
  host     : process.env.RDS_HOSTNAME,
  user     : process.env.RDS_USERNAME,
  password : process.env.RDS_PASSWORD,
  port     : process.env.RDS_PORT,
  database : process.env.RDS_DATABASE
});
exports.handler = (event, context, callback) => {
  // allows for using callbacks as finish/error-handlers
  context.callbackWaitsForEmptyEventLoop = false;
  const sql = "INSERT INTO MESSAGE (message) VALUES ('I am MySQL')";
  con.query(sql, (err, res) => {
    if (err) {
      throw err
    }
    callback(null, '1 records inserted.');
  })
};

I have double checked the security group settings and it seems to be correct我已经仔细检查了安全组设置,它似乎是正确的

I have been referring to我一直在提到

`` https://medium.com/@hk_it_er/create-lambda-and-api-gateway-nodejs-aws-serverless-to-rds-mysql-6a75243e61cc `` https://medium.com/@hk_it_er/create-lambda-and-api-gateway-nodejs-aws-serverless-to-rds-mysql-6a75243e61cc

`` ``

Any pointers as to what is wrong or what should i check ?关于什么是错误的或我应该检查什么的任何指示?

PS: In a day after 100 attempts i have managed to insert 3 rows with the same code and settings. PS:在 100 次尝试后的一天内,我设法使用相同的代码和设置插入了 3 行。

the issue was with node v 10. I switched to node 8 and it seems to be working with ease.问题出在节点 v 10。我切换到节点 8,它似乎很容易工作。 Guess node 10 has made some calls async.猜猜节点 10 进行了一些异步调用。

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

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