0

I need to create a lambda function to act as the middleman between a mobile Java app and an AWS RDS MySQL database. The idea is to submit queries from the mobile app and then send them off to the lambda function, which will then return the query. I have a basic MySQL query set up in my AWS lambda:

var mysql = require('mysql');
var config = require('./config.json');
var pool  = mysql.createPool({
  host            : config.dbhost,
  user            : config.dbuser,
  password        : config.dbpassword,
  database        : config.dbname
});

exports.handler = (event, context, callback) -> {
    context.callbackWaitsForEmptyEventLoop = false;
    pool.getConnection(function(err, connection) {
      if (err) throw err; // not connected!

      // Use the connection
      connection.query('select Album from record', function (error, results, fields) {
        // When done with the connection, release it.
        connection.release();

        // Handle error after the release.
        if (error) callback(error);
        else callback(null, results[0].Album);


        // Don't use the connection here, it has been returned to the pool.
      });
    });
};

And all that I am currently trying to do is get this code to run and output what the query will return. I've seen tutorials where people seem to just click test and have the code run, but it keeps asking me to create a test, and I'm not sure what exactly I would need to do to test this function.

EDIT: I realized I was missing a small change in my lambda uploaded code, but I am now getting an error on line 10 saying there is an unexpected token >.

I'm not sure what's wrong here, as the tutorial I watched seems to have the same exact thing.

8
  • If you're only returning one album, maybe you should add a LIMIT to the query. Commented Sep 18, 2018 at 16:39
  • 1
    Change -> to => Commented Sep 18, 2018 at 16:42
  • That got rid of the coding error but now it's getting a timeout error. Commented Sep 18, 2018 at 16:49
  • 1
    The Lambda function default timeout is 3 seconds so you may need to increase that. In general, you're going to have to decide how to deal with timeouts because they are not uncommon with DB connections. Also, get used to adding console.log() statements and using CloudWatch Logs to view the logging output afterwards. Commented Sep 18, 2018 at 16:55
  • Upping the timeout limit lets it run for a bit longer, but it still fails, giving the errors "Process exited before completing request" and "Handshake inactivity timeout". I'm seeing some other threads that say it's likely a code error, so I'm currently looking it over again. Commented Sep 18, 2018 at 17:11

1 Answer 1

4

Since you're not passing in any parameters through the context, you can just create a test with the defaults or an empty object {}, and click Test in the console. It will invoke your Lambda function as if it had been called from your mobile app, and you can debug from there.

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

Comments

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.