Thursday, 20 April 2017

How to use AWS Lambda to send SES notification email w.r.t. S3 events


AWS Lambda + SES notification +S3 events

Image title

Hello Buddies.
Sometimes we want to get notifications when an event occurs in AWS S3 bucket like a file upload, deletion, etc.
For the same the event handling capability of AWS Lambda can come handy, and we will involve SES (Amazon Simple Email Service) in this demo.

To realize this, we will follow the following simple steps -

1.    Have an S3 bucket in place -
It will look like as follows -







Here for example sake, we will use the bucket listed as ‘namit’.

2.    Next comes the lambda function for playing its role, of an event listener -
Lambda     -->    New function


3.    You can refer to the below code for replacing the Lambda template (Node js) -


var aws = require('aws-sdk');

var ses = new aws.SES({

    region: 'us-west-2'

});

const s3 = new aws.S3({
    apiVersion: '2006-03-01'
});

exports.handler = function(event, context, callback) {

    console.log("Incoming: ", event);

    const bucket = event.Records[0].s3.bucket.name;

    const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));

    const news = `Event took place in - ${bucket} -> ${key}`;

    const params = {

        Bucket: bucket,

        Key: key,

    };

    var eParams = {

        Destination: {

            ToAddresses: ["testingxxxxx@gmail.com"]

        },

        Message: {

            Body: {

                Text: {

                    Data: `${news}`

                }

            },

            Subject: {

                Data: "Email Notification"

            }

        },

        Source: "testingxxxxx@gmail.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(data);

            console.log("EMAIL CODE END");

            console.log('EMAIL: ', email);

            context.succeed(event);

        }

    });

};



Make sure you replace the email id with the one you intent to use. 
A brief look at the configuration and trigger for the lambda -
















When enabled, Lambda function will start receiving the event notifications from the S3 it is bound too.

Alongwith the above code some policy and permission changes are required as well.
For the above sample, we can use the below policy structure, in IAM -

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents", "ses:SendEmail", "ses:SendRawEmail" ], "Resource": "*" } ] }

And make sure the email ID we are using should be verified first in SES -









Uploading a sample file in the S3 bucket 'namit' -> green.jpg











Check the cloudwatch and the email account used.

BINGO
We have got the mail with -

Subject    : Email Notification
Body        : Event took place in - namit -> green.jpg

Let’s disable the lambda function now, and give a new try with some different use case. :)

Happy Coding!!

Featured post

JAVA based project, that can be used to hit DB using JDBC, from WSO2 ESB

Hi Buddies, Here is a small project that will enable you to hit MySQL DB using WSO2 ESB - https://github.com/namitsharma99/customM...

Popular Posts