{"id":18867,"date":"2015-04-10T14:46:54","date_gmt":"2015-04-10T09:16:54","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18867"},"modified":"2015-04-10T14:46:54","modified_gmt":"2015-04-10T09:16:54","slug":"aws-lambda-invocation-using-amazon-s3-invocation","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aws-lambda-invocation-using-amazon-s3-invocation\/","title":{"rendered":"AWS Lambda Invocation using Amazon S3"},"content":{"rendered":"<p>To start, we create a Lambda function to consume events published by Amazon S3. For any object uploaded to a bucket, S3 will invoke our Lambda function by passing event information in the form of function parameters. AWS Lambda executes the function. As the function executes, it reads the S3 event data, logs some of the event information to Amazon CloudWatch. This is an example of &#8220;push&#8221; model where Amazon S3 invokes the Lambda function.So, let&#8217;s get started with AWS Lambda Amazon S3 Invocation.<br \/>\n<\/br>For more information on the &#8220;push&#8221; model, see\u00a0<a href=\"https:\/\/www.google.co.in\/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=2&amp;cad=rja&amp;uact=8&amp;ved=0CCMQFjAB&amp;url=http%3A%2F%2Fdocs.aws.amazon.com%2Flambda%2Flatest%2Fdg%2Flambda-dg.pdf&amp;ei=hG0nVbqUJoPRsAWuooTIDQ&amp;usg=AFQjCNG_YpPEd_H224Gd6FijDZDn6xKT5A\">AWS Lambda: How it Works<\/a><\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">DEMO<\/p>\n<p>In this Demo, we will do the following:<\/p>\n<ul>\n<li>Create a Lambda function to process Amazon S3 events and test it by invoking it manually by using sample Amazon S3 event data. The function reads incoming event data and writes logs to Amazon CloudWatch.<\/li>\n<li>Configure an S3 bucket notification so that Amazon S3 can publish object-created events to AWS Lambda by invoking your Lambda function.<\/li>\n<\/ul>\n<p><\/br>We can then upload objects to the bucket. Each object upload will cause Amazon S3 to invoke the Lambda function. You can verify AWS Lambda executed your function by reviewing the logs.<br \/>\n<\/br><strong>Important<\/strong><br \/>\nBoth the Lambda function and the S3 bucket must be in the same AWS region. This exercise assumes the\u00a0us-east-1 (Virginia)\u00a0region.<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Step 1: Create a Lambda Function and Invoke it Manually (Using the Console)<\/h3>\n<p><\/br>In this step, we create and invoke a Lambda function:<\/p>\n<p style=\"text-align: justify;color: #0000FF\">Go to the AWS Management Console, select &#8220;Lambda&#8221; service &amp; click on &#8220;Create a Lambda Function&#8221;.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LLi.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18871\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LLi.png\" alt=\"LLi\" width=\"1873\" height=\"765\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Select the following options:<\/p>\n<p style=\"text-align: justify;color: #0000FF\">Name: Give a appropriate name to Lambda Function ( we have named it S3)<\/p>\n<p style=\"text-align: justify;color: #0000FF\">Description: This should ideally tell what the function will do.<\/p>\n<p style=\"text-align: justify;color: #0000FF\">Code entry type: <em>Edit Code inline<\/em><\/p>\n<p style=\"text-align: justify;color: #0000FF\">Code Template: <em>None<\/em> (from the drop-down menu)<\/p>\n<ul type=\"disc\">\n<li><span style=\"font-family: verdana, arial, sans-serif\"><span style=\"font-size: small\"><span style=\"color: #000000\">We will use the following JavaScript code that console provides to create a Lambda function. The code simply reads the Amazon S3 event data it receives as parameter and logs some of the information to Amazon CloudWatch Logs. The &#8220;console.log()&#8221; statements in the code generate log events in CloudWatch.<\/span><\/span><\/span><\/li>\n<\/ul>\n<p><\/br><\/p>\n<pre class=\"programlisting\">console.log('Loading function');\r\nvar aws = require('aws-sdk');\r\nvar s3 = new aws.S3({apiVersion: '2006-03-01'});\r\n\r\nexports.handler = function(event, context) {\r\n    console.log('Received event:', JSON.stringify(event, null, 2));\r\n    \/\/ Get the object from the event and show its content type\r\n    var bucket = event.Records[0].s3.bucket.name;\r\n    var key = event.Records[0].s3.object.key;\r\n    s3.getObject({Bucket: bucket, Key: key}, function(err, data) {\r\n        if (err) {\r\n            console.log(\"Error getting object \" + key + \" from bucket \" + bucket +\r\n                \". Make sure they exist and your bucket is in the same region as this function.\");\r\n            context.fail (\"Error getting file: \" + err)      \r\n        } else {\r\n            console.log('CONTENT TYPE:', data.ContentType);\r\n            context.succeed();\r\n        }\r\n    });\r\n};<\/pre>\n<ul type=\"disc\">\n\t<\/br><\/p>\n<li class=\"programlisting\">\n<p style=\"text-align: justify;color: #0000FF\">Role*: S3 Execution role (Under Create a new role)A new window will open up, give the appropriate name to the Role (we have named it as\u00a0 ( lambda_S3_exec_role) &amp; click on &#8220;allow&#8221;.<\/li>\n<\/ul>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LL1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18872\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LL1.png\" alt=\"LL1\" width=\"1011\" height=\"608\" \/><\/a><\/p>\n<p style=\"text-align: justify;color: #0000FF\">Click on &#8220;Create Lambda Function&#8221;.<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Step 2: Configure Amazon S3 as the Event Source\u00a0<\/span><\/b><\/span><\/span><\/h1>\n<p><span style=\"color: #000000\"><span style=\"font-family: verdana, arial, sans-serif\"><span style=\"font-size: small\">In this step, we add Amazon S3 as the event source for Lambda function. We will be using a already created S3 Bucket &#8220;s3lambda&#8221;. The service Lambda does the following:<\/span><\/span><\/span><\/p>\n<ul type=\"disc\">\n<li><span style=\"font-family: verdana, arial, sans-serif\"><span style=\"font-size: small\"><span style=\"color: #000000\">Adds a permission, in the access policy associated with Lambda function, to allow S3 to invoke the function.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #000000\"><span style=\"font-family: verdana, arial, sans-serif\"><span style=\"font-size: small\">Adds notification configuration on the S3 bucket identifying the event type (object-created events) and Lambda function. When \u00a0S3 detects the event of the specified type, it invokes the Lambda function by passing event information as function parameters.\u00a0<\/span><\/span><\/span><\/li>\n<\/ul>\n<p><\/br><\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Step 2.1: Add Amazon S3 as the Event Source<\/span><\/b><\/span><\/span><\/h2>\n<p><span style=\"font-family: verdana, arial, sans-serif\"><span style=\"font-size: small\"><span style=\"color: #000000\">Follow the steps to add Amazon S3 as the event source for our Lambda function.\u00a0<\/span><\/span><\/span><\/p>\n<ol>\n<li><span style=\"font-family: verdana, arial, sans-serif\"><span style=\"font-size: small\"><span style=\"color: #000000\">In the AWS Lambda console, add an event source for our Lambda function.<\/span><\/span><\/span>\n<ol type=\"a\">\n<li>\n<p style=\"text-align: justify;color: #0000FF\">On the\u00a0<b>Lambda: Function List<\/b>\u00a0page, click the triangle to the left of the function name to view expanded function details.<a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LL2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18876\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LL21.png\" alt=\"LL2\" width=\"1913\" height=\"1080\" \/><\/li>\n<p style=\"text-align: justify;color: #0000FF\">b. From the Actions drop-down, select Add Event Source.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LL3.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18874\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LL3.png\" alt=\"LL3\" width=\"1901\" height=\"417\" \/><\/a><\/li>\n<li>\n<p style=\"text-align: justify;color: #0000FF\">In the dialog box that opens, select &#8220;S3&#8221; from the\u00a0<b>Event source type<\/b>\u00a0drop-down.<\/span><\/span><\/span><\/li>\n<li>\n<p style=\"text-align: justify;color: #0000FF\">Select the bucket &#8220;s3lambda&#8221; from the\u00a0<b>Bucket<\/b>\u00a0drop-down. (In your case, it has to be the bucket you created)<\/span><\/span><\/span><\/li>\n<li>\n<p style=\"text-align: justify;color: #0000FF\">Select\u00a0<b>Object Created<\/b>\u00a0from the\u00a0<b>Event type<\/b>\u00a0drop-down.<\/span><\/span><\/span><\/li>\n<li>\n<p style=\"text-align: justify;color: #0000FF\">Click\u00a0<b>Submit<\/b>.This will add Amazon S3 as the event source for the Lambda function. The console will also do the following to complete the setup:<\/span><\/span><\/span><\/p>\n<ol type=\"disc\">\n<li><span style=\"font-family: verdana, arial, sans-serif\"><span style=\"font-size: small\"><span style=\"color: #000000\">Add a permission in the function access policy to grant Amazon S3 principal permission to invoke the function.<\/span><\/span><\/span><\/li>\n<li><span style=\"font-family: verdana, arial, sans-serif\"><span style=\"font-size: small\"><span style=\"color: #000000\">Add notification configuration on the S3 bucket so that Amazon S3 can publish object-created events to AWS Lambda by invoking the specific function.<\/span><\/span><\/span><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>You will see the following screen:<a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LL4.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18875\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LL4.png\" alt=\"LL4\" width=\"1862\" height=\"66\" \/><\/a><\/p>\n<div class=\"titlepage\">\n<div>\n<div>\n<h3 style=\"text-align: justify;color: #ff9900\">Step 2.2: Test the Setup\n<\/div>\n<\/div>\n<\/div>\n<p><\/br>To test the setup, we will do the following:<\/p>\n<div class=\"procedure\">\n<ol class=\"procedure\" type=\"1\">\n<li class=\"step\">Upload an object to the S3 bucket. Amazon S3 will detect the object-created event and invoke our Lambda function.AWS Lambda will then execute our function. As the function executes, it reads Amazon S3 event data it received as parameters, and logs some of the event information to CloudWatch Logs.<\/li>\n<li class=\"step\">You can verify your logs by using the following steps:\n<ol class=\"substeps\" type=\"a\">\n<li class=\"step\">Review function logs in the AWS Lambda console. For the specific function, the logs appear under the <span class=\"guilabel\">CloudWatch Metrics at a glance<\/span> heading.<\/li>\n<li class=\"step\">You can click the <span class=\"guilabel\">logs<\/span> links to open review the logs in the CloudWatch console.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/div>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LL-Success.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18877\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/LL-Success.png\" alt=\"LL Success\" width=\"1875\" height=\"795\" \/><\/a><\/p>\n<p>The console will look like this, once a object is uploaded in the bucket.<\/p>\n<p style=\"text-align: justify;color: #0000FF\">\nInvocation count           : Number of invocations done by S3 on Lambda Function.<br \/>\nInvocation duration        : Duration of the invocation.<br \/>\nInvocation failures        : Number of invocations failed by S3 on Lambda Function.<br \/>\nThrottled invocations      : Request that gets invoked asynchronously can absorb reasonable bursts of traffic for approximately 15-30 minutes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To start, we create a Lambda function to consume events published by Amazon S3. For any object uploaded to a bucket, S3 will invoke our Lambda function by passing event information in the form of function parameters. AWS Lambda executes the function. As the function executes, it reads the S3 event data, logs some of [&hellip;]<\/p>\n","protected":false},"author":166,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":19},"categories":[1174,1],"tags":[248,1679,1332,1742,1741,1545,670],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18867"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/166"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18867"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18867\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}