{"id":2527,"date":"2019-05-19T12:01:37","date_gmt":"2019-05-19T16:01:37","guid":{"rendered":"http:\/\/blog.domb.net\/?p=2527"},"modified":"2019-05-20T09:21:58","modified_gmt":"2019-05-20T13:21:58","slug":"aws-serverless-workshop-image-processing-module-5-extra-credit","status":"publish","type":"post","link":"https:\/\/blog.domb.net\/?p=2527","title":{"rendered":"AWS Serverless Workshop Image Processing Module 5 (extra credit)"},"content":{"rendered":"<p>If you are interested and new to serverless, then the https:\/\/github.com\/aws-samples\/aws-serverless-workshops\/ is definitely worth your time.<\/p>\n<p>This post will walk you through how you can get the extra points in module 5.<\/p>\n<p>Here is what you are supposed to achieve once you finish step 5:<\/p>\n<p>&#8220;The intent of the\u00a0<strong>PhotoDoesNotMeetRequirement<\/strong> 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<strong> NotificationPlaceHolderFunction<\/strong> 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).&#8221;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2528\" src=\"http:\/\/blog.domb.net\/wp-content\/uploads\/Screen-Shot-2019-05-19-at-11.35.46-AM.png\" alt=\"\" width=\"665\" height=\"528\" srcset=\"https:\/\/blog.domb.net\/wp-content\/uploads\/Screen-Shot-2019-05-19-at-11.35.46-AM.png 665w, https:\/\/blog.domb.net\/wp-content\/uploads\/Screen-Shot-2019-05-19-at-11.35.46-AM-300x238.png 300w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/p>\n<p>Open SES in the AWS console and register your email address. Make sure you validate it.<\/p>\n<p>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 <strong>wildrydes-step-module-res-NotificationPlaceholderF-CVX2KHND<\/strong>\u00a0role.<\/p>\n<pre class=\"lang:default decode:true \">{\r\n    \"Version\": \"2012-10-17\",\r\n    \"Statement\": [\r\n        {\r\n            \"Effect\": \"Allow\",\r\n            \"Action\": [\r\n                \"logs:CreateLogGroup\",\r\n                \"logs:CreateLogStream\",\r\n                \"logs:PutLogEvents\"\r\n            ],\r\n            \"Resource\": \"arn:aws:logs:*:*:*\"\r\n        },\r\n        {\r\n            \"Effect\": \"Allow\",\r\n            \"Action\": [\r\n                \"ses:SendEmail\",\r\n                \"ses:SendRawEmail\"\r\n            ],\r\n            \"Resource\": \"*\"\r\n        }\r\n    ]\r\n}<\/pre>\n<p>Once you&#8217;ve attached the policy to the role you can go ahead and modify the lambda function named <strong><em>wildrydes-step-module-res-NotificationPlaceholderF.\u00a0 <\/em><\/strong>You should now see the following in the designer (note the Amazon SES).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2529\" src=\"http:\/\/blog.domb.net\/wp-content\/uploads\/Screen-Shot-2019-05-19-at-11.41.23-AM.png\" alt=\"\" width=\"1510\" height=\"484\" srcset=\"https:\/\/blog.domb.net\/wp-content\/uploads\/Screen-Shot-2019-05-19-at-11.41.23-AM.png 1510w, https:\/\/blog.domb.net\/wp-content\/uploads\/Screen-Shot-2019-05-19-at-11.41.23-AM-300x96.png 300w, https:\/\/blog.domb.net\/wp-content\/uploads\/Screen-Shot-2019-05-19-at-11.41.23-AM-768x246.png 768w, https:\/\/blog.domb.net\/wp-content\/uploads\/Screen-Shot-2019-05-19-at-11.41.23-AM-1024x328.png 1024w\" sizes=\"auto, (max-width: 1510px) 100vw, 1510px\" \/><\/p>\n<p>Modify the index.js as follows<\/p>\n<pre class=\"lang:java decode:true \">var aws = require('aws-sdk');\r\nvar ses = new aws.SES({\r\n   region: 'us-east-1'\r\n});\r\n\r\n'use strict';\r\n\r\nconst util = require('util');\r\n\r\nexports.handler = (event, context, callback) =&gt; {\r\n\r\n    console.log(\"Reading input from event:\\n\", util.inspect(event, {depth: 5}));\r\n\r\n    var title = \"\";\r\n    var message = \"\";\r\n    var emailto = event.emailAddr;\r\n    \r\n    console.log('Email var:\\n', emailto)\r\n\r\n    if (event[\"errorInfo\"]) {\r\n        title = event[\"errorInfo\"][\"Error\"];\r\n        var errorCause = JSON.parse(event[\"errorInfo\"][\"Cause\"]);\r\n        message = errorCause[\"errorMessage\"];\r\n    }\r\n    \r\n        var eParams = {\r\n        Destination: {\r\n            ToAddresses: [emailto]\r\n        },\r\n        Message: {\r\n            Body: {\r\n                Text: {\r\n                    Data: message\r\n                }\r\n            },\r\n            Subject: {\r\n                Data: title\r\n            }\r\n        },\r\n        Source: \"test@me.com\"\r\n    };\r\n    \r\n    callback(null, {\"messageToSend\": {\"title\": title, \"message\": message}})\r\n    \r\n    console.log('=== sending email===');\r\n    var email = ses.sendEmail(eParams, function(err, data){\r\n        if(err) console.log(err);\r\n        else {\r\n            console.log(\"=== email is on the way ===\");\r\n            console.log(data);\r\n            console.log(\"email done\");\r\n            console.log('EMAIL: ', email);\r\n            context.succeed(event);\r\n\r\n        }\r\n    });\r\n\r\n};<\/pre>\n<p>Go to your stepfunction RiderPhotoProcessing and execute a new run. Don&#8217;t forget to add the new parameter emailAddr<\/p>\n<pre class=\"lang:default decode:true \">{\r\n  \"userId\": \"user_a\",\r\n  \"s3Bucket\": \"wildrydes-step-module-resource-riderphotos3bucket-ccrq\",\r\n  \"s3Key\": \"1_happy_face.jpg\",\r\n  \"emailAddr\": \"my@email.com\"\r\n}<\/pre>\n<p>If\u00a0 the step functions enters the state PhotoDoesNotMeetRequirements you will receive an email with the explanation why the step function entered this state.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: &#8220;The intent of the\u00a0PhotoDoesNotMeetRequirement step is to send notification to&#8230;<\/p>\n","protected":false},"author":2,"featured_media":2531,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[161],"tags":[108,163,162,164],"class_list":["post-2527","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","tag-aws","tag-lambda","tag-step-function","tag-wildrydes"],"_links":{"self":[{"href":"https:\/\/blog.domb.net\/index.php?rest_route=\/wp\/v2\/posts\/2527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.domb.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.domb.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.domb.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.domb.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2527"}],"version-history":[{"count":3,"href":"https:\/\/blog.domb.net\/index.php?rest_route=\/wp\/v2\/posts\/2527\/revisions"}],"predecessor-version":[{"id":2575,"href":"https:\/\/blog.domb.net\/index.php?rest_route=\/wp\/v2\/posts\/2527\/revisions\/2575"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.domb.net\/index.php?rest_route=\/wp\/v2\/media\/2531"}],"wp:attachment":[{"href":"https:\/\/blog.domb.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.domb.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.domb.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}