Sending Exception Detail as Email through AWS SES

Amazon Simple Email Service (Amazon SES) is a highly scalable and cost-effective email service for developers and businesses. Amazon SES eliminates the complexity and expenditure of building an in-house email solution or licensing, installing, and operating a third-party email service for this type of email communication. In addition, the service integrates with other AWS services, making it easy to send emails from applications being hosted on AWS.


  1. AWS SES
  2. AWS SNS
  3. AWS S3
  4. AWS Lambda


When a user uploads an excel file to web console, each row data from the excel file should save into the database. If this process got failed due to some exception, the exception error message must go to the configured email Id’s.


We are using two lambda functions. One is used for uploading excel sheet to s3 bucket and another for sending exceptional email to administrator.

When we are uploading excel sheet to s3 bucket, if any failure or exception occurs in that time, Excel upload lambda function will send the error result to SNS (Simple Notification Service) and Exception email lambda function will trigger the SNS and send an exceptional email to the administrator by using SES.


Setting up Prerequisites


Create a SNS Topic by following below steps,

  1. Open AWS account à choose services as SNS
  2. Click on Create Topic à give the Topic name and Display Name
  3. Click on Create topic

2) AWS S3 Bucket

Create an AWS S3 Bucket with add permission and enable CORS configuration.

3) AWS Lambda

Two Lambda functions are required:

i) For reading excel file and saving each row data into concerned database

a)   Create the lambda function using below code:

var s3file = s3.getObject(params,function (err,data){


result.message = 'error while getting ' + key + 'from' + bucketName +' bucket name';

result.describtion = err;;


var wstream = fs.createWriteStream('/tmp/user.xlsx');

wstream.write(data.Body , function(err){



} else{


input: '/tmp/user.xlsx',

output: null,

},function(err, rest) {

if(err) {

result.message = 'error while reading the'+key+ ' file from '+bucketName;

} else{

maxCount = rest.length;

console.log('max count f rows in excel/cvs file = ' + maxCount);

ExcelData = rest;



b)   Upload the Zip file containing NodeJS Code, which holds logic for reading the excel file and pushing each row data into the concerned database by calling custom API.

c)   Map AWS Lambda Trigger to AWS S3 when putObject() method invoked inside AWS S3.


ii) To send the exception email

a)   Create lambda function using below code:

var ses = new aws.SES({

apiVersion: '2010-12-01'


// Generating params to send Email

var params = {

Destination: {},

Message: {

Subject: {


Charset: 'UTF-8'


Body: {

Html: {

Data: message,

Charset: 'UTF-8'





params.Destination.ToAddresses = [emailTOAddress];

params.Source = FromAddress;

// calling send email function

ses.sendEmail(params, function (err, data) {

if (err) {//failure message

result.message = err, err.stack;


} else {//Sucess

result.message = 'Email send successfully'; = data;





b)  Map AWS Lambda Trigger to AWS SNS topic when any exception occurs in the first lambda function i.e. while saving each row of excel file into the database.


Step 1: Upload the excel file directly into AWS S3 bucket manually or through AWS SDK

Step 2: If everything goes well, all the records from excel file will be saved into database. Else an email will go to the configured (admin) email id with exception details


Based on the configuration and given excel file, records will be saved into the database and if any exception occurs then it will go as an email to the admin (configured email).


Automating Deployment of AWS Lambda

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second.

Deploying Codes into AWS Lambda through AWS Web Console is insecure and time taking, since every time user needs to login into AWS Lambda console and then they have to upload the zip file or need to provide AWS S3 file path manually. To overcome this issue, there is a very simpler way to automate the deployment process as shown in the below flow chart.


Setup and Configuration:

Step 1: Create an AWS Lambda function and deploy the below zip file into it,

Step 2: Enable Lambda trigger with AWS S3 bucket when putObject method is invoked. Both S3 and Lambda Function should be in same region.

Step 3: Enable Versioning inside AWS S3 Bucket.

Step 4: Create a config.json file using any of the below configurations,

a) For Deployment into a new Lambda Function:


"accessKeyId" : "< AccessKeyId >",

"secretAccessKey": < SecretAccessKey >",

"region": "<Region>",

"lambdaFunctionName" : "<LamdaFunctionName>",

"lambdaFunctionType":  "new",


"lambdaRole":"<ARN Name>",




b) For Deployment into an old Lambda Function:


"accessKeyId" : "< AccessKeyId >",

"secretAccessKey": < SecretAccessKey >",

"region": "<Region>",

"lambdaFunctionName" : "<LamdaFunctionName>",

"lambdaFunctionType":  "old"


Step 5: Make a zip file containing following file(s)

a) Lambda code written in nodejs (index.js)

b) Node Modules folder (node_modules)

c) Other Relevant files


Deploying into AWS Lambda:

You can deploy the lambda code into any of the existing Lambda function or a new Lambda function anytime.

Steps needs to follow

Step 1: Push config.json file into AWS S3 bucket where trigger event mapping is done.

Step 2: Make the visibility of config.json file as public.

Step 3: Push <Lambda>.zip file into AWS S3.

Step 4: Deployment of lambda function will be done according to your config.json file. To verify it, go to the AWS Cloud Watch Console and go through the logs.

Note:  Copying files into AWS S3 can be done in two ways,

a) Copying file by login into AWS S3 Web Console.

b) By invoking putObject() method by using AWS SDK (Platform Independent).


The Zip file pushed into AWS S3 will be deployed into AWS lambda according to the configuration file (config.json). The automation of lambda function deployment can be configured for any AWS account.