Greg Pagendam-Turner Greg Pagendam-Turner - 2 years ago 210
SQL Question

Lambda AWS not calling node mysql callbacks

I am trying to process bounces sent from Amazon's Simple Email Service via their Simple Notification Service vi a Lambda on AWS.

I'm running the following script:

var aws = require('aws-sdk');

var mysql = require('mysql');

Processor = {};

Processor.initializeConnection = function() {
console.log('Connecting to database');
Processor.connection = mysql.createConnection({
host : 'MYHOST',
user : 'MYUSER',
password : 'PASSWORD',
database : 'DATABASE'
console.log('Connection configured');
Processor.connection.connect(function(err) {
if (err != null) {
console.log('Could not connect to database');
return false;
} else {
console.log('Successfully connected to database');
return true;

console.log('Should not get here');

exports.handler = function(event,context){
console.log('Received event:');
var message = event.Records[0].Sns.Message;

// Get the object from the event and show its content type

if(Processor.initializeConnection()) {'Database connection failed');


I upload this script as index.js along with node_modules containing the node mysql module all as a zip file.

I get the following output from Amazon when this is run:

START RequestId: 378b8a8c-30d4-11e5-9db4-9b9537e3f53d
2015-07-23T00:46:13.159Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Received event:
2015-07-23T00:46:13.160Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Connecting to database
2015-07-23T00:46:14.035Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Connection configured
2015-07-23T00:46:14.095Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Should not get here
END RequestId: 378b8a8c-30d4-11e5-9db4-9b9537e3f53d
REPORT RequestId: 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Duration: 937.51 ms Billed Duration: 1000 ms Memory Size: 128 MB Max Memory Used: 14 MB

None of the code inside the connect fallback is run. I'm expecting it to report a connection failure as I'm not using valid credentials.

If I run a version of the code locally under nodejs the connect callback does fire. It just doesn't fire under Lambda.

Answer Source

Due to the asynchronous nature of node.js, your code might be exiting as a result of context.succeed() before all of your functions are executed.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download