Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using AWS SES for Exception Emails

DZone's Guide to

Using AWS SES for Exception Emails

This quick and easy guide uses AWS Lambda, S3, and SES to send out emails detailing errors when trying to add in database information from an Excel file.

· Cloud Zone
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

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

Requirements

When a user uploads an Excel file to the web console, each row data from the Excel file should save into the database. If this process fails due to some exception, the exception message must go to the configured email IDs.

Workflow

We are using two lambda functions. One is used for uploading the Excel sheet to an S3 bucket and another for sending exception emails to the administrator.

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

Solution

Let's set up our prerequisites:

AWS SNS

Create an SNS Topic by following the steps below:

  1. Open your AWS account and choose the SNS service.
  2. Click on Create Topic and give a Topic Name and Display Name.
  3. Click on Create topic.

AWS S3 Bucket

Create an AWS S3 bucket and enable CORS configuration.

AWS Lambda

Two lambda functions are required:

Lambda 1: For reading the Excel file and saving each row of data into the concerned database:

  • Create the lambda function using the code below:

  • 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;
            }
        }
    }


  • Upload the ZIP file containing the Node.js code, which holds the logic for reading the Excel file and pushing each row of data into the concerned database by calling a custom API.
  • Map the AWS Lambda trigger to AWS S3 — when the putObject() method is invoked inside AWS S3.

Lambda 2: To send the exception email.

  • Create a lambda function using the code 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);
        }
    });

  • Map the AWS Lambda trigger to an AWS SNS topic when any exception occurs in the first lambda function — i.e. while saving each row of the Excel file into the database.

Implementation

Step 1: Upload the Excel file directly into the AWS S3 bucket either manually or through the AWS SDK.

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

Conclusion

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

For More details please visit : http://vmokshagroup.com/blog/

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

Topics:
aws lambda ,aws s3 ,amazon ses ,tutorial ,cloud ,exception notifications

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}