An Introduction to AWS Lambda

Posted By : Ankit Uniyal | 26-Jun-2017

In this blog, we will be discussing about AWS Lamda and its core features and we also go through an example in which we will create a AWS Lambda Nodejs function and upload code on AWS lambda console.


But before going further, let's start with a brief introduction about what is AWS Lambda and what it can provide us so that we can achieve "Run Code Not Servers" tag line.


AWS Lambda is basically a compute service provided by Amason run in Amazon Web service.We can build serverless app using AWS Lambda function which triggered by events. It is a "Function as a Service" platform which allows developers to build serverless app and do not focus about servers.


Now, below is the example which provides how to create a lambda function and deploy your code on AWS Lambda console.


We are creating a Lamda function function which resize an Image and convert image into required image type and then package the code in ZIP file and upload on Lambda console.


We have used below Javascript file which contains our event handler code.


ResizedImage.js - contains event handler code which resize an Image using 'Sharp" Nodejs module 


We also customized package.json file which contains configurations and dependencies details for project and in addition to that we also package the dependencies along with our code in ZIP file and upload on AWS Lambda console.


We are using 'Sharp' Nodejs module for Image resize which also converts image into JPEG, PNG or any other image type.

resizedImageLambda.js :

var sharprequire("sharp");

exports.imageResize = function(event, context,callback) {
    console.log('QueryString Parameters:', event.queryStringParameters);

var imageData = event.queryStringParameters.inputBuffer;

sharp(imageData).resize(143, 200).toFormat('jpg').toBuffer(function(err,data) {
          if(err) {
            console.log("Error in image resze");
          }

        else {

          callback(data);

         }   
});

}

package.json

{
  "name": "imageResizeLambda",
  "version": "1.0.0",
  "description": "",
  "main": "resizedImageLambda.js",
  "dependencies": {
   "sharp":"0.18.1"
  }
}

Next, we will create a deployment pacjage which is a ZIP file which includes Javascript file and dependencies which required for our Project. The root level of ZIP file contains JS file, node modules directory and package.json file.

In this example, our deployment ZIP file looks like :

resizedImageLambda.js

package.json

node_modules/sharp

Make sure, you have to compress contents of directory not the directory itself.
 

Below are the steps to upload your Lambda code in AWS Lambda console:
 

1.After logging into AWS console, click on Create a lambda function

2.Click Skip on Select Blueprint page

3.Then Enter function name and then add short description in Description field and set run time to Nodejs.

4.In Lambda function code, choose upload a ZIP file, and upload your deployment ZIP.

5.Now, enter the handler name like resizedImageLambda.resizeImage as the resizedImageLambda is your file name which contains your lambda handler and resizeImage is your handler name.
 

Thanks

About Author

Author Image
Ankit Uniyal

Ankit has knowledge in Javascript, NodeJS, AngularJS and MongoDB also have experience in using AWS Services.

Request for Proposal

Name is required

Comment is required

Sending message..