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.

Prerequisites

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

Requirement

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.

Workflow

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.

Solution

Setting up Prerequisites

1) AWS SNS

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){

if(err){

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

result.describtion = err;

context.fail(result);

}else{

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

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

if(err){

console.log(err);

} else{

exceltojson({

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: {

Data: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;

console.log(result);

context.fail(result);

} else {//Sucess

result.message = 'Email send successfully';

result.data = data;

console.log(result);

context.succeed(result);

}

});

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.

Implementation

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

Conclusion

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).

 

FacebookTwitterGoogle+Share
About Pragyan Panda

Pragyan panda is a software developer at Vmoksha Technologies who have outstanding knowledge in the .net domain. She also has work experience in AWS services.She is a person who is positive about every aspect of her life.Apart from this, she likes to spend her time by preparing food and listening music.



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>