44

I have created a very basic simple function on AWS Lambda which will be used to accept form submissions.

Part of the function will be to send an email to a particular person, pretty simple. I am trying to use AWS SES in order to do this. I have setup the SES service etc, and verified the account I wish to send to and have been able to send out a test email. All works!!

Now when I try and do the same within AWS Lambda and use the aws sdk it doesn't send out the email. I don't get an error or anything.

Below is the code that I am using for the AWS Lambda function.

Has anyone had any experience using lambda and sending emails via ses, through a lambda function? Or even just using the node.js aws sdk would more than likely be helpful.

var aws = require('aws-sdk');
var ses = new aws.SES({
   accessKeyId: 'myAccessKey',
   secretAccesskey: 'mySecretKey',
   region: 'eu-west-1' 
});

exports.handler = function(event, context) {
    console.log("Incoming: ", event);
    var output = querystring.parse(event);

    var eParams = {
        Destination: {
            ToAddresses: ["toAddress@email.com"]
        },
        Message: {
            Body: {
                Text: {
                    Data: output.Key1
                }
            },
            Subject: {
                Data: "Ses Test Email"
            }
        },
        Source: "mysource@source.com"
    };

    console.log('===SENDING EMAIL===');
    var email = ses.sendEmail(eParams, function(err, data){
        if(err) console.log(err);
        else {
            console.log("===EMAIL SENT===");
            console.log(data);
        }
    });
    console.log("EMAIL CODE END");
    console.log('EMAIL: ', email);
    context.succeed(event);
};
Darren
  • 795
  • 1
  • 6
  • 10

4 Answers4

29

It would appear that I had the context.succeed(event) placed in the wrong area of code.

Once I moved it into the sendEmail callback all worked.

var aws = require('aws-sdk');
var ses = new aws.SES({
  accessKeyId: 'myAccessKey',
  secretAccesskey: 'mySecretKey',
  region: 'eu-west-1' 
});

exports.handler = function(event, context) {
  console.log("Incoming: ", event);
  var output = querystring.parse(event);

  var eParams = {
    Destination: {
        ToAddresses: ["toAddress@email.com"]
    },
    Message: {
        Body: {
            Text: {
                Data: output.Key1
            }
        },
        Subject: {
            Data: "Ses Test Email"
        }
    },
    Source: "mysource@source.com"
};

console.log('===SENDING EMAIL===');
var email = ses.sendEmail(eParams, function(err, data){
    if(err) {
       console.log(err);
       context.fail(err);
    } else {
        console.log("===EMAIL SENT===");
        console.log("EMAIL CODE END");
        console.log('EMAIL: ', email);
        console.log(data);
        context.succeed(event);
    }
});};
Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34
Darren
  • 795
  • 1
  • 6
  • 10
  • Darren> Have you tried Java equivalent of the above? – Jasper Oct 13 '15 at 04:51
  • @Jasper unfortunately I haven't. Have you experienced the same issue? – Darren Oct 14 '15 at 06:09
  • Invoking this works... via rest endpoints from a program running on my computer..... but if i try from some other domain (server) - i see 403 (could be due to cross-domain CORS thing). – Jasper Oct 14 '15 at 10:42
  • @Jasper - I would need to know a little more about what you are trying to do. Have you asked a question on stackoverflow. If you include the detail of what you are trying to do and how you are achieving it in a question I can have a look at it. – Darren Oct 15 '15 at 07:59
  • I tried invoking this via a simple HTML HTTP Post:
    But console.log incoming is always empty! Although i can invoke this via a Java code successfully!
    – Jasper Mar 18 '16 at 05:11
  • pls see this question: http://stackoverflow.com/questions/36052740/sending-a-http-post-request-correctly – Jasper Mar 18 '16 at 05:13
  • @Jasper Have you tried to send this using an Ajax call. – Darren Mar 21 '16 at 09:58
  • @Darren, just use IAM roles instead of the the access keys would be more better from security standpoint as well as from key management. – Kartik Narayana Maringanti Dec 04 '17 at 13:38
  • thanks @Darren, It works perfect. Just fix the extra semi colon :if(err) { console.log(err); context.fail(err); };<=== – Jorge Valvert Oct 19 '18 at 22:05
1
var aws = require("aws-sdk");
var ses = new aws.SES({ region: "us-west-2" });
exports.handler = async function (event) {
  var params = {
    Destination: {
      ToAddresses: ["RecipientEmailAddress", ...],
    },
    Message: {
      Body: {
        Text: { Data: "Test" },
      },

      Subject: { Data: "Test Email" },
    },
    Source: "SourceEmailAddress",
  };

  return ses.sendEmail(params).promise()
};
0

This is because Lambda freezes the container when the function exits and any async processes are frozen, such as your email. This is especially true with Node. See Lambda Programming Model. http://docs.aws.amazon.com/lambda/latest/dg/lambda-introduction.html

halt00
  • 336
  • 1
  • 3
  • 16
0

My case is: when you set VPC, the issue happens cause of internet limitation access.

If you remove VPC, everything works fine.

It seems a AWS bug for me.

I opened today a AWS Support for it.

No anwers yet.

Julian Corrêa
  • 638
  • 12
  • 25