0

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. 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

``

Any pointers as to what is wrong or what should i check ?

P.S: In a day after 100 attempts i have managed to insert 3 rows with the same code and settings.

Ersoy
  • 8,816
  • 6
  • 34
  • 48
Doe
  • 11
  • 4
  • You found anything on log? – Nghia Do May 27 '19 at 17:44
  • Possible duplicate of [node.js async/await using with MySQL](https://stackoverflow.com/questions/44004418/node-js-async-await-using-with-mysql) – JD D May 28 '19 at 02:12
  • your lambda handler exits before the query can be run since the query is async... take a look at the other Q linked above... I think using `util.promisify` and `async`/`await` keywords should help you assuming you are using the latest version of node – JD D May 28 '19 at 02:15

1 Answers1

0

the issue was with node v 10. I switched to node 8 and it seems to be working with ease. Guess node 10 has made some calls async.

Doe
  • 11
  • 4