Home > Uncategorized > Send email using #NodeJS with #AWS #Lambda and #SES

Send email using #NodeJS with #AWS #Lambda and #SES


Amazon Lamdba is a great service for quickly deploying NodeJS apps, without worrying about dealing with servers. Great if your servers are strictly production only, and you need to have a “throw away” but internet-connected API.

So, I created a NodeJS Lambda function on AWS, triggered using the API gateway with no authentication. You need to set up AWS SES with a verified email address, and get your SMTP credentials ready.

Here’s the code I used;

var aws = require(‘aws-sdk’);
var ses = new aws.SES({
accessKeyId: ‘xxxxxx’,
secretAccesskey: ‘xxxxx’,
region: ‘eu-west-1’

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

var eParams = {
Destination: {
ToAddresses: [“xxxx.xxxx@gmail.com”]
Message: {
Body: {
Text: {
Data: JSON.stringify(event)
Subject: {
Data: “Ses Test Email”
Source: “info@xxxxxxx.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(“EMAIL CODE END”);
console.log(‘EMAIL: ‘, email);
var response = {
statusCode: 200,
body: “Sending email”
callback(null, response);

When running this for the first time I got the error

AccessDenied: User `arn:aws:sts::005445879168:assumed-role/helloWorldRole/LambdaMail’ is not authorized to perform `ses:SendEmail’

Which meant that I had to log in to IAM, and attach the policy AmazonSESFullAccess to the Role “helloWorldRole”

The format of the email sent was:

“resource”: “\/LambdaMail”,
“path”: “\/LambdaMail”,
“httpMethod”: “GET”,
“headers”: {
“CloudFront-Forwarded-Proto”: “https”,
“CloudFront-Is-Desktop-Viewer”: “true”,
“CloudFront-Is-Mobile-Viewer”: “false”,
“CloudFront-Is-SmartTV-Viewer”: “false”,
“CloudFront-Is-Tablet-Viewer”: “false”,
“CloudFront-Viewer-Country”: “DE”,
“Host”: “0vuu0520rb.execute-api.eu-west-1.amazonaws.com”,
“Via”: “1.1 d2e34d11a094aa8f0c8077cfdf5b4b38.cloudfront.net (CloudFront)”,
“X-Amz-Cf-Id”: “uWqPSOmAwMbgRDiwh8Wtwigf_YRHyXYM2CnC1tj-NzXOvk287KXs6Q==”,
“X-Amzn-Trace-Id”: “Root=1-58dd11fa-6ead550f5465a3814fc53748”,
“X-Forwarded-For”: “,”,
“X-Forwarded-Port”: “443”,
“X-Forwarded-Proto”: “https”
“queryStringParameters”: {
“FirstName”: “Fiach”,
“Reference”: “GPhxXsQ”,
“Address2”: “10 NUALAMONT DRIVE”,
“Country”: “United Kingdom”,
“Address1”: “10 Nualamont Drive”,
“City”: “Derry”,
“LastName”: “Reid”,
“Postcode”: “BT48 9PH”
“pathParameters”: null,
“stageVariables”: null,
“requestContext”: {
“accountId”: “005445879168”,
“resourceId”: “i6b7on”,
“stage”: “prod”,
“requestId”: “b745b5a7-1552-11e7-8da2-c13b9a89bb97”,
“identity”: {
“cognitoIdentityPoolId”: null,
“accountId”: null,
“cognitoIdentityId”: null,
“caller”: null,
“apiKey”: null,
“sourceIp”: “”,
“accessKey”: null,
“cognitoAuthenticationType”: null,
“cognitoAuthenticationProvider”: null,
“userArn”: null,
“userAgent”: null,
“user”: null
“resourcePath”: “\/LambdaMail”,
“httpMethod”: “GET”,
“apiId”: “0vuu0520rb”
“body”: null,
“isBase64Encoded”: false

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: