title: AWS Lambda Node Function Silently Fails
body: <p>I have an AWS Lambda function served through the AWS API gateway. Everything seems to work fine for the most part. There is one function of our API that isn't working right. It is a webhook that receives incoming faxes and the emails us to let us know it came in.</p>
  
  <p>We have tested everything and <strong>this works perfectly</strong> on our local machine and even on providers like linode or digital ocean. Our problem is only when deployed with AWS Lambda.</p>
  
  <ul>
  <li>I have tried increasing memory and execution limits well beyond what is required.</li>
  <li>I've tried with multiple email methods and libraries.</li>
  <li>I've tried with various function formats.</li>
  </ul>
  
  <p>The cloudwatch logs for lambda show the <code>console.log(req.body.MediaUrl);</code> but there is no other output on stdout or stderr. Even if I put obvious flaws into it there will be no errors. It's almost as if the function silently fails without explaining why. </p>
  
  <p>I have been able to get it to work if I send 2-3 post requests all within about 1-2 seconds of another. 1 maybe 2 emails will be sent, but never all of them. If a single post request is sent as is normal usage you never get an email. </p>
  
  <pre><code>// Define a handler for when the fax is initially sent
  const nodemailer = require('nodemailer');
  const aws = require('aws-sdk');
  
  // Start Email Logic
  // AWS access keys are built into Lambda, modify permissions of the executor role
  aws.config.update({region: 'us-west-2'});
  
  let transporter = nodemailer.createTransport({
    SES: new aws.SES({
      apiVersion: '2010-12-01',
    }),
   });
  
  exports.received = function(req, res) {
  
    transporter.sendMail({
    from: '"Fax" &lt;fax@domain.com&gt;',
    to: process.env.FAX_ADDRESS,
    subject: 'A new fax from ' + req.body.From,
    text: 'You can view the fax at this url:\n\n' + req.body.MediaUrl,
  }, (err, info) =&gt; {
    if (err) {
      console.log(err);
    } else {
      console.log(info.envelope);
      console.log(info.messageId);
      console.log('Email Sent');
    }
  });
  
  // log the URL of the PDF received in the fax just in case email fails
  console.log(req.body.MediaUrl);
  
    res.status(200);
    res.send();
  };
  </code></pre>
  
  <p>BTW the above code has another file with all the proper express items to properly serve this. Again, it works perfectly on local, other servers and even sometimes when flooding lambda; it never consistently works on Lambda though.</p>
  
  <p>My guess is either I'm an idiot that needs to sleep or I'm missing something about Lambda here. Can anyone shed some light on what I'm missing?</p>
  
  <h2>EDIT :</h2>
  
  <p>Thanks to Michael and Hen things fell into place. When working with Lambda the lambda function will shut down the moment it gets through the main process logic. It won't wait for pending asynchronous items to complete. The following change fixed my issue when using Express with Lambda.</p>
  
  <p>Notice that I've moved the mail functions to the end of the response and then moved the response status and send to inside of the mail function. This is a one of many ways this can be solved but is sweet and simple for our needs.</p>
  
  <pre><code>exports.received = function(req, res) {
  
    // log the URL of the PDF received in the fax just in case email fails
    console.log(req.body.MediaUrl);
  
    transporter.sendMail({
    from: '"Fax" &lt;fax@domain.com&gt;',
    to: process.env.FAX_ADDRESS,
    subject: 'A new fax from ' + req.body.From,
    text: 'You can view the fax at this url:\n\n' + req.body.MediaUrl,
  }, (err, info) =&gt; {
    if (err) {
      console.log(err);
      res.status(500);
      res.send();
    } else {
      console.log(info.envelope);
      console.log(info.messageId);
      console.log('Email Sent');
      res.status(200);
      res.send();
    }
  });
  };
  </code></pre>