0

I have developed a node application which I would like to have called as an AWS Lambda Application.

The application works as intended as an AWS Lambda, however my CloudWatch logs always finish with the following error: Process exited before completing request.

I wrote some code to ensure that my context.succeed() and context.fail() calls were taking place and they are. However, when ran locally, I also noted a lag between my logging of success in start.js and the command prompt appearing again, making me believe there could be some node processes still taking place once those calls have been made. Could that be causing the error, and if so what is a good way to triage and resolve the issue?

The relevant code is below:

lambda-handle.js

import log from './log';
import database from './database';
import User from './database/models/user';

export function handle(event, context) {
  log.info('CS Blogs Feed Aggregator Started');

  database.sync()
    .then(() =>
      User.findAll({
        attributes: ['id', 'blogFeedURI']
      }))
    .then(users => {
      users.forEach(user => {
        log.info({ user }, 'User loaded from database');
      });
    })
    .then(() => {
      // context.done() called so AWS knows function completed successfully
      context.succeed();
    })
    .catch(error => {
      context.fail(error);
    });
}

start.js (used to test context.succeed/fail being called)

// This function invokes the AWS Lambda Handle as AWS would
// but allows you to do it from your local machine for development
// or from a non-AWS server

import { handle } from './lambda-handle';
import log from './log';

handle({}, {
  succeed: result => {
    log.info({ result: result || 'No result returned' }, 'Process succeeded');
  },
  fail: error => {
    log.error({ error: error || 'No error returned' }, 'Process failed');
  }
});

The code is being transpiled by babel before being deployed. However, I suspect there is more of a logic issue so I have shown you the original source code.

If any more information is required the repository is available here: https://github.com/csblogs/feed-downloader/tree/fix/lambda-implementation-details

Thanks

dannybrown
  • 1,083
  • 3
  • 13
  • 26
  • Does `User.findAll()` return a promise? If not it seems like you would be trying to iterate on `users` before its there. – Ryan Feb 14 '16 at 04:41
  • Yes. User.findAll() returns a promise and the code works as intended. – dannybrown Feb 14 '16 at 11:59
  • Then I would check the following: 1. Set an extra high timeout on the Lambda, maybe it's timing out 2. Install node 0.10.x locally and make sure the code still runs 3. If the database is an aws resource make sure your lambda has the right role(s) to access it – Ryan Feb 14 '16 at 17:17
  • @Ryan. The timeout on the lambda is already 5 minutes. Far longer than execution takes. The code is running as intended as a lambda as shown by the logs. The database is an AWS resource but is reachable as the users are correctly printed in the logs. – dannybrown Feb 14 '16 at 17:30

1 Answers1

0

I am pretty sure this is caused by at least 1 native module dependency in bunyan(dtrace-provider). Native modules need to be built/installed on the system that they will run on. So in the case of Lambda you need to run npm install on a linux ec2 instance or possibly vagrant to get the right version of dtrace-provider built.

See: Cross-compile node module with native bindings with node-gyp https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/ (scroll to Native Modules)

You could probably just remove Bunyan to verify it works and then go down the ec2/vagrant compile route if that fixes it.

Community
  • 1
  • 1
Ryan
  • 5,845
  • 32
  • 27