1

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) {
                console.log('****');
                console.log(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()) {
                context.fail('Database connection failed');
                return;
        }

    context.succeed(message);
};

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.

1
  • Your callbacks are structured incorrectly. You need to pass your callback inside initializeConnection() and call it from inside it. Commented Jul 23, 2015 at 17:13

1 Answer 1

2

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.

See:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.