AWS Serverless Workshop Image Processing Module 5 (extra credit)

If you are interested and new to serverless, then the https://github.com/aws-samples/aws-serverless-workshops/ is definitely worth your time.

This post will walk you through how you can get the extra points in module 5.

Here is what you are supposed to achieve once you finish step 5:

“The intent of the PhotoDoesNotMeetRequirement step is to send notification to the user that the verification of their profile photo failed so they might try uploading a different picture. It currently uses the AWS Lambda function NotificationPlaceHolderFunction which simply returns the message instead of actually sending the notification. Implement sending email notifications in the Lambda function using Amazon Simple Email Service (SES).”

Open SES in the AWS console and register your email address. Make sure you validate it.

Then you need to make sure that your lambda function is allowed to call SES. This means you will need to add a policy to your wildrydes-step-module-res-NotificationPlaceholderF-CVX2KHND role.

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

Once you’ve attached the policy to the role you can go ahead and modify the lambda function named wildrydes-step-module-res-NotificationPlaceholderF.  You should now see the following in the designer (note the Amazon SES).

Modify the index.js as follows

var aws = require('aws-sdk');
var ses = new aws.SES({
   region: 'us-east-1'
});

'use strict';

const util = require('util');

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

    console.log("Reading input from event:\n", util.inspect(event, {depth: 5}));

    var title = "";
    var message = "";
    var emailto = event.emailAddr;
    
    console.log('Email var:\n', emailto)

    if (event["errorInfo"]) {
        title = event["errorInfo"]["Error"];
        var errorCause = JSON.parse(event["errorInfo"]["Cause"]);
        message = errorCause["errorMessage"];
    }
    
        var eParams = {
        Destination: {
            ToAddresses: [emailto]
        },
        Message: {
            Body: {
                Text: {
                    Data: message
                }
            },
            Subject: {
                Data: title
            }
        },
        Source: "test@me.com"
    };
    
    callback(null, {"messageToSend": {"title": title, "message": message}})
    
    console.log('=== sending email===');
    var email = ses.sendEmail(eParams, function(err, data){
        if(err) console.log(err);
        else {
            console.log("=== email is on the way ===");
            console.log(data);
            console.log("email done");
            console.log('EMAIL: ', email);
            context.succeed(event);

        }
    });

};

Go to your stepfunction RiderPhotoProcessing and execute a new run. Don’t forget to add the new parameter emailAddr

{
  "userId": "user_a",
  "s3Bucket": "wildrydes-step-module-resource-riderphotos3bucket-ccrq",
  "s3Key": "1_happy_face.jpg",
  "emailAddr": "my@email.com"
}

If  the step functions enters the state PhotoDoesNotMeetRequirements you will receive an email with the explanation why the step function entered this state.

 

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.