1
votes

I'm setting up a simple NodeJS Lambda function on AWS to send emails.

The code below is working when I run it locally. I have received all the credentials from AWS, verified both sender and recipient emails and granted all permissions to SES for my Lamba

const aws = require("aws-sdk");
const config = require('../config')

aws.config.update({
  accessKeyId: config.mailUser,
  secretAccessKey: config.mailPassword,
  region:'us-east-1'
});

const ses = new aws.SES({apiVersion: '2010-12-01'});

const sendEmail = async (mailOptions) => {
  const {
    from,
    to,
    subject,
    html
  } = mailOptions
  console.log('foo')
  ses.sendEmail({
    Source: from,
    Destination: {
      ToAddresses: [to]
    },
    Message: {
      Subject: {
        Data: subject,
        Charset: 'UTF-8'
      },
      Body: {
        Html: {
          Data: html,
          Charset: 'UTF-8'
        }
      }
    }
  }, 
  (err, data) => {
    console.log('baz')
    if (err) {
      console.error(err);
    } else {
      console.log('Email sent:');
      console.log(data);
    }
  });
};
console.log('bar')
module.exports = { 
  sendEmail 
};

It seems that ses.sendEmail() never fires when the function is deployed - I get foo and bar in the CloudWatch logs but never baz. Again, everything is running smoothly if run locally.
What is it that I am missing?

1
First guess is it's a role issue. What roles have you attached to your Lambda and what is showing in the CloudWatch logs?K Mo
I currently have this in the role policy, which looks as if it should suffice (maybe it doesn't) ` { "Sid": "VisualEditor2", "Effect": "Allow", "Action": "ses:*", "Resource": "*" } ` By now I have put logs on almost every line in my code and they all fire but for the part in ses.sendMail() callbackIvan Makarov

1 Answers

1
votes

https://www.reddit.com/r/aws/comments/bf2iss/lambda_function_not_able_to_send_email_using_ses/elb8vzr/

Here is a very good explanation - apparently you need to wrap your ses.sendMail call in a promise for it to work with AWS Lambda.

All credit goes to https://www.reddit.com/user/jsdfkljdsafdsu980p/ and https://www.reddit.com/user/Enoxice/