Home > Software design >  Async call to SES not working in AWS Lambda
Async call to SES not working in AWS Lambda

Time:10-16

I have created a Lambda function which contains the following JavaScript;

var AWS = require('aws-sdk');
var ses = new AWS.SES({ region: "us-east-1"});
var RECEIVER = '[email protected]';
var SENDER = '[email protected]';
var response = {
 "statusCode": 200,
 "headers": { 
"Content-Type": "application/json","Access-Control-Allow-Origin": "*"
},
"isBase64Encoded": false,
 "body": "{ \"result\": \"Success\"\n}"
}
exports.handler = async function (event, context) {
    console.log('Received event:', event);
    sendEmail(event, function (err, data) {
        context.done(err, null);
    });
return response;
};
function sendEmail (event, done) {
    var params = {
        Destination: {
            ToAddresses: [
                RECEIVER
            ]
        },
        Message: {
            Body: {
                Text: {
                    Data: 'name: '   event.name   '\nphone: '   event.phone   '\nemail: '   event.email   '\ndesc: '   event.desc,
                    Charset: 'UTF-8'
                }
            },
            Subject: {
                Data: 'Website Referral Form: '   event.name,
                Charset: 'UTF-8'
            }
        },
        Source: SENDER
    };
    ses.sendEmail(params).promise();
}

The function gets triggered by an amazon (aws) REST api, the api gets the data from a html contact form. This works and the function gets the data successfully. The function is set up to use SES service to send the email. The function has full permissions and roles, the email addresses are verified and I'm not in the SES sandbox, I have increased the function timeout and cloudwatch logs suggest the function is working correctly Logs

Why am I still not receiving emails? My intuition suggests it is something to do with the Javascript code. I would be very grateful for any help I have been trying to figure this out for a long time. The function did actually work once, I received the email successfully but thats the only time.

CodePudding user response:

This is a common mistake made when using promise-based API methods in AWS Lambda functions.

You need to await the results of the SES sendEmail call in your handler, or your lambda function will exit and the email will not get sent.

One way to modify your code would be something like:

exports.handler = async function(event, context) {
  console.log('Received event:', event);
  const response = await sendEmail(event, function(err, data) {
    context.done(err, null);
  });
// Probably you should be returning an object with a status code here...I'll leave that to you to figure out!
  return response;
};

async function sendEmail(event, done) {
  var params = {
    Destination: {
      ToAddresses: [
        RECEIVER
      ]
    },
    Message: {
      Body: {
        Text: {
          Data: 'name: '   event.name   '\nphone: '   event.phone   '\nemail: '   event.email   '\ndesc: '   event.desc,
          Charset: 'UTF-8'
        }
      },
      Subject: {
        Data: 'Website Referral Form: '   event.name,
        Charset: 'UTF-8'
      }
    },
    Source: SENDER
  };
  const response = await ses.sendEmail(params).promise();
  return response;
}
  • Related