I've created a basic lambda function that should send an email from the SES service, but it is not working.
I've followed this tutorial: here. I've created the proper IAM policy and attached it to the function, but I don't think that's the problem.
I've created a test config for the function with and the control flow is not reaching inside of sendEmail, and I'm not sure why.
const aws = require('aws-sdk')
const ses = new aws.SES({ region: 'us-east-1' });
exports.handler = async (event, context, callback) => {
const printOut = JSON.stringify(event);
console.log(`Received event: ${printOut}`);
const params = {
Destination: {
ToAddresses: ["xxxxx.xxxx@gmail.com"]
},
Message: {
Body: {
Text: {
Data: event['desc']
}
},
Subject: {
Data: "Email from xxxxxxx Contact Us Form"
}
},
Source: "xxxxxxxxxxxxxxxxx@gmail.com"
};
console.log(`sending: ${JSON.stringify(params)}`);
ses.sendEmail(params, function (err, data) {
callback(null, { err: err, data: data });
if (err) {
console.error(err);
context.fail(err);
} else {
console.log(data);
context.succeed(event);
}
console.log(`Done!`);
});
};
Here is my current output:
Response:
null
Request ID:
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx"
Function Logs:
START RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx Version: $LATEST
2019-11-12T01:28:48.352Z xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx INFO Received event: {"name":"Redacted","email":"xxxxx.xxxx@gmail.com","desc":"Test message"}
2019-11-12T01:28:48.372Z xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx INFO sending: {"Destination":{"ToAddresses":["xxxxx.xxxx@gmail.com"]},"Message":{"Body":{"Text":{"Data":"Test message"}},"Subject":{"Data":"Email from xxxxxx Contact Us Form"}},"Source":"xxxxxxxxxxxxxxx@gmail.com"}
END RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx
REPORT RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx Duration: 590.10 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 94 MB Init Duration: 359.55 ms
As you can see, the console outs/errors inside the sendEmail function are never being called. Not really sure why.
I did make sure to verify both of the email addresses I'm testing with, but no dice.
You have to either make it non-async or return a promise back to the caller (you can return that promise directly to the runtime). Like this:
const aws = require('aws-sdk')
const ses = new aws.SES({region: 'us-east-1'});
exports.handler = async (event, context, callback) => {
const printOut = JSON.stringify(event);
console.log(`Received event: ${printOut}`);
const params = {
Destination: {
ToAddresses: ["xxxxx.xxxx@gmail.com"]
},
Message: {
Body: {
Text: {
Data: event['desc']
}
},
Subject: {
Data: "Email from xxxxxxx Contact Us Form"
}
},
Source: "xxxxxxxxxxxxxxxxx@gmail.com"
};
console.log(`sending: ${JSON.stringify(params)}`);
return ses.sendEmail(params, null).promise();
};