2

The company I work for is currently trying to migrate some of our existing codebase over to AWS Lambda. We are running node.js 4.3 (highest version offered for lambda) and we are executing inside a VPC to connect to an RDS database. The Lambda function is connecting to the database just fine, however a simple select query to our MySQL table isn't executing or returning any errors. Here is the code we are trying:

exports.handler = (event, context, callback) => {

  /**
   * Require Config file
  **/
  var config = require('./config.js');

  /**
   * AWS/MWS Configuration
  **/
  var AWS = require('aws-sdk');
  var MWS = require('mws');

  var mws           = require('./lib/mws-reports/lib/mws.js');
  var XML           = require('./lib/mws-reports/pixl-xml');
  var mwsReportsAPI = require('./lib/mws-reports/lib/reports.js');
  var mwsFeedsAPI   = require('./lib/mws-reports/lib/feeds.js');

  AWS.config.region      = config.AWS.region;
  AWS.config.credentials = config.AWS.credentials;

  var client = new mws.Client(
    config.MWS.credentials.accessKeyId,
    config.MWS.credentials.secretAccessKey,
    config.MWS.credentials.sellerID,
    {}
  );

  /**
   * Configure DB
   * @var Promise - A library to maintain Promises (used for chained MySQL queries)
   * @var Utils - A wrapper for utility functions.
   * @var connection - The MySQL DB Connection
  **/
  var Promise = require('bluebird');
  var utils   = require('./lib/Utils');
  var mysql = require('mysql');

  var MySQLConnection = mysql.createConnection(config.mysql);

  MySQLConnection.connect(function (err) {
    if(err) {
      console.log("Error connection: " + err.stack);
      return;
    }

    console.log("Connected as id " + connection.threadId);
  });

  var connection      = Promise.promisifyAll(MySQLConnection);
  var InventoryHealth = require('./app/models/InventoryHealth');
  var Items           = require('./app/models/Items');

  connection.query("SELECT * FROM items", function(err, rows) {
    rows.forEach(function(item) {
      console.log(JSON.stringify(item));
    });
  });

  connection.end();
  context.done(null, "Finished :)");
}

As I mentioned, the strangest part of all of this is the lack of response I get from Lambda. This is all it tells me:

START RequestId: f726f0ba-ecec-11e6-b0b3-9d51c554a5ac Version: $LATEST
2017-02-07T04:22:01.123Z    f726f0ba-ecec-11e6-b0b3-9d51c554a5ac    (node) crypto.createCredentials is deprecated. Use tls.createSecureContext instead.
END RequestId: f726f0ba-ecec-11e6-b0b3-9d51c554a5ac
REPORT RequestId: f726f0ba-ecec-11e6-b0b3-9d51c554a5ac  Duration: 4232.01 ms    Billed Duration: 4300 ms    Memory Size: 128 MB Max Memory Used: 22 MB

Any advice would be amazing. Thank you.

1
  • 2
    First: All the initialization of modules you should do outside your handler function. Second: You need to read up on how callbacks work. You are not waiting for your callback to be called before you call context.done. My suggestion is to read up on javascript/node callbacks and how to use those. Commented Feb 7, 2017 at 9:23

1 Answer 1

1

You are not using callbacks correctly. Read up on that. And remember to initialize modules outside the handler for faster function execution.

Without looking at what the code does, and I have not tested this code, you need something more in the lines of:

  /**
   * Require Config file
  **/
  var config = require('./config.js');

  /**
   * AWS/MWS Configuration
  **/
  var AWS = require('aws-sdk');
  var MWS = require('mws');

  var mws           = require('./lib/mws-reports/lib/mws.js');
  var XML           = require('./lib/mws-reports/pixl-xml');
  var mwsReportsAPI = require('./lib/mws-reports/lib/reports.js');
  var mwsFeedsAPI   = require('./lib/mws-reports/lib/feeds.js');

  AWS.config.region      = config.AWS.region;
  AWS.config.credentials = config.AWS.credentials;

  var client = new mws.Client(
    config.MWS.credentials.accessKeyId,
    config.MWS.credentials.secretAccessKey,
    config.MWS.credentials.sellerID,
    {}
  );

  /**
   * Configure DB
   * @var Promise - A library to maintain Promises (used for chained MySQL queries)
   * @var Utils - A wrapper for utility functions.
   * @var connection - The MySQL DB Connection
  **/
  var Promise = require('bluebird');
  var utils   = require('./lib/Utils');
  var mysql = require('mysql');

  var InventoryHealth = require('./app/models/InventoryHealth');
  var Items           = require('./app/models/Items');

  exports.handler = (event, context, callback) => {

    var MySQLConnection = mysql.createConnection(config.mysql);

    MySQLConnection.connect(function (err) {
      if(err) {
        console.log("Error connection: " + err.stack);
        callback(err, err);
      } else {

        console.log("Connected as id " + connection.threadId);

        var connection      = Promise.promisifyAll(MySQLConnection);


        connection.query("SELECT * FROM items", function(err, rows) {
          if (err) {
            console.log(err);
            callback(err, err);
          } else {
            rows.forEach(function(item) {
              console.log(JSON.stringify(item));
            });
            connection.end();
            callback(null, rows);
          }
        });


      }
});


}
Sign up to request clarification or add additional context in comments.

1 Comment

This worked, thank you so much! I'll be sure to read a little more on callbacks.

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.