DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Using AWS Lambda to Send SES Notifications Triggered by S3 Events

Using AWS Lambda to Send SES Notifications Triggered by S3 Events

AWS Lambda's event handling abilities make it ideal to serve as a serverless listener. Events from your S3 bucket can be used to send email alerts with ease.

Namit Sharma user avatar by
Namit Sharma
·
Apr. 21, 17 · Tutorial
Like (5)
Save
Tweet
Share
13.61K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

Sometimes we want to get notifications when an event occurs in an AWS S3 bucket, like a file upload, deletion, etc.

The event handling capability of AWS Lambda can come handy in such a situation, and we will involve SES (Amazon Simple Email Service) in this demo.

Let's go through the steps.

The S3 Bucket

Have an S3 bucket in place. It will look like:

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

The Lambda Function

Next comes the Lambda function. It will play the role of an event listener:

Lambda     -->    New function

The Code

You can refer to the below code to replace 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 intend to use. Next, a brief look at the configuration and trigger for the Lambda:

When enabled, the Lambda function will start receiving event notifications from the S3 bucket it is bound to. Along with the above code, some policy and permission changes are required as well.

For the above sample, we can use the following 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 has been verified first in SES:


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

Check the CloudWatch and the email account used. 

BINGO! We have mail with:

Subject: Email Notification

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

Let’s disable the Lambda function now, and we'll think of a different use case.

AWS AWS Lambda Event

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Express Hibernate Queries as Type-Safe Java Streams
  • How To Validate Three Common Document Types in Python
  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • Using the PostgreSQL Pager With MariaDB Xpand

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: