I may be missing something simple, but have run out of things to try/test.

I have two (nearly identical) nodemail functions that I am trying to test under AWS Lambda, one uses the callback invocation, one uses the newer async invocation. When I test locally (using "Lambda-Local") both work exactly as expected (i.e. I receive the expected email). When I test in Lambda the callback version works as expected, while the async version fails silently - no error, no indications at all.

Note: I've been using nodemail for ages in production under Lambda via callbacks with no issues, I was just motivated to try the new async exports.handler wrapper....

Async Version (fails silently):

'use strict'

var aws = require("aws-sdk");
var nodemailer = require("nodemailer");

const ses = new aws.SES({ region: 'us-east-1' });

exports.handler = async (event, context) => {

    var mailOptions = {
        from: "[email protected]",
        subject: "This is an email sent from a Lambda function using async!",
        html: `<p>Test message</p>`,
        to: "[email protected]",
    };

    // create Nodemailer SES transporter
    var transporter = nodemailer.createTransport({
        SES: ses
    });

    // send email
    transporter.sendMail(mailOptions)
    .then( results => {

        console.log("Success:", results);

        return {
            statusCode: 200,
            body: JSON.stringify({ "API Success": results }, null),
            headers: {
               'Access-Control-Allow-Origin': '*',
           },
        };    

    })
    .catch ( error => {
        console.log("Error:", error);

        return {
            statusCode: 500,
            body: JSON.stringify({ "API Error": error }, null),
            headers: {
               'Access-Control-Allow-Origin': '*',
           },
        };    

    })

};

Callback Version (works as expected):

'use strict'

var aws = require("aws-sdk");
var nodemailer = require("nodemailer");

const ses = new aws.SES({ region: 'us-east-1' });

exports.handler = function (event, context, callback) {

    var mailOptions = {
        from: "[email protected]",
        subject: "This is an email sent from a Lambda function using callbacks!",
        html: `<p>Test message</p>`,
        to: "[email protected]",
    };

    // create Nodemailer SES transporter
    var transporter = nodemailer.createTransport({
        SES: ses
    });

    // send email
    transporter.sendMail(mailOptions)
    .then( results => {

        console.log("Success:", results);

        callback(null, {
            statusCode: 200,
            body: JSON.stringify(results),
            headers: {
               'Access-Control-Allow-Origin': '*',
           },
        });  

    })
    .catch ( error => {
        console.log("Error:", error);

        callback(null, {
            statusCode: 500,
            body: JSON.stringify(error),
            headers: {
               'Access-Control-Allow-Origin': '*',
           },
        });  

    })

};

0 Answers