2
votes

Amazon Simple Email Service (Amazon SES)

I have the below code. It works perfectly if I use it from an aws ec2 instance or from my workstation. But as soon as I add it to a lambda function I am working on inside of an AWS VPC, the callback to my ses.sendEmail() never gets called. I never see a "sendEmail Function Error", or a "sendEmail Function Success" console.log() in my CloudWatch Logs for the function and my lambda function times out at the end of my timeout period. I don't know what more I can do.

I have looked for any IAM roles or policies that I might have to add, can't find any mention of them needed, or which ones to add.

Tried adding the Policy 'AmazonSESFullAccess' to my IAM role for the function. Still Times out.

let aws = require('aws-sdk')
    , ses = new aws.SES({ apiVersion: '2010-12-01', region: 'us-west-2' })
    ;

sendEmail({
    To : [ '[email protected]' , '[email protected]'],
    From: '[email protected]',
    Subject: 'Sending An Email Out',
    Body: `<html> A Buch of HTML Here</html>`
}, function(err, result){
    if(err){
        console.error('SendEmail Error', err);
    } else {
        console.log('SendEmail Result', result);
    }
});


function sendEmail(emailObj, cb){
    emailObj.To.push('[email protected]');

    let mailData = {
        Source: emailObj.From,
        Destination: { ToAddresses: emailObj.To },
        Message: {
            Subject: {
                Data: emailObj.Subject
            },
            Body: {
                Html: {
                    Data: emailObj.Body
                }
            }
        }
    }

    console.log('sending Email', JSON.stringify(mailData)); //see in Cloudwatch Logs! YEAH!!!
    ses.sendEmail(mailData, function(err, data){
        if(err){
            console.log('sendEmail Function Error', JSON.stringify(err)); // never see in cloudwatch logs, WHAT??
            cb(err);
        } else {
            console.log('sendEmail Function Success', JSON.stringify(data)); // never see in cloudwatch logs, WHAT??
            cb(null, data);
        }
    });
}
1

1 Answers

5
votes

You're lambda is set up inside a VPC, which in that case - has no access to the internet, thus no access to AWS APIs as well.

If you let your lambda run long enough - the callback will be called with a connection timeout error.

Please follow my detailed answer here.