What is AWS Lambda? & AWS Lambda Use Case
What is AWS Lambda? & AWS Lambda Use Case

What is AWS Lambda? & AWS Lambda Use Case

What is AWS Lambda? & AWS Lambda Use Case

What is AWS Lambda? & AWS Lambda Use Case

AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services (AWS). It is a computing service that runs code in response to events and automatically manages the computing resources required by that code. You need not bother about what Amazon resources to launch or how to manage them. You just put the code on Lambda and run it.  It’s that simple!

How does AWS Lambda work?

The code that you want Lambda to run is known as Lambda Function. The function responds to the “Events” such as object uploads to Amazon S3 bucket, updates to dynamo DB table, in-app purchases, etc. For example, these “Events” can be ‘Create Events’ like PUT, POST, COPY object from or to S3 Bucket.

You can create a Lambda function by providing your code as a zip file or you can design in an Integrated Development Environment (IDE) in the AWS Management Console.

How to setup AWS Lambda?

You can set up your code to automatically trigger from AWS such as Amazon API Gateway, or call it directly from your web or mobile app.

AWS Lambda Use Case: Resizing images uploaded to Amazon S3

Let’s take a simple use case of uploading an image to Amazon S3 and resizing the image (using the Lambda function) to support mobile, tablet, and desktop devices.

‘File upload to S3’ event triggers the Lambda function & then executes the code to resize the uploaded image.

Here are the steps:

  • A user uploads an image using the web or mobile app. The image is saved in the Amazon S3 bucket.
  • ‘Create Event’ is triggered once the image upload to S3 is successful.
  • The event calls/invokes the configured Lambda function.
  • The code inside the lambda function is executed for resizing the images.
  • The resized images will be stored in S3.
AWS Lambda Work Flow

Code Snippet

1) Make sure you have all your dependencies

// dependencies
var async = require('async');
var AWS = require('aws-sdk');
var gm = require('gm')
.subClass({ imageMagick: true }); 
// Enable ImageMagick integration.
var util = require('util');

2) Define constants and create AWS S3 Client

// constants
var MAX_WIDTH = 100;
var MAX_HEIGHT = 100;
// get reference to S3 client 
var s3 = new AWS.S3();

3) The following handler will check for the newly uploaded image and resize it to the desired sizes.

a) When the image gets uploaded to source bucket the required metadata is fetched from the source bucket of that object/image

exports.handler = function(event, context) {
      // Read options from the event.
      var srcBucket = event.Records[0].s3.bucket.name;
      var srcKey = event.Records[0].s3.object.key;
      var dstBucket = srcBucket + "-resized";
      var dstKey = srcKey;

      // Sanity check: validate that source and destination are different buckets.
      if (srcBucket == dstBucket) {
          console.error("Destination bucket must not match source bucket.");
          return;
       }

       // Infer the image type.
       var typeMatch = srcKey.match(/\.([^.]*)$/);
       if (!typeMatch) {
           console.error('unable to infer image type for key ' + srcKey);
           return;
       }
       var imageType = typeMatch[1];
       if (imageType != "jpg" && imageType != "png") {
           console.log('skipping non-image ' + srcKey);
           return;
        }

b) Once the object gets passed for sanity and validation checks, the next process is to download, transform the image to desired sizes and then upload to destination bucket as defined in the code using async waterfall

// Download the image from S3, transform, and upload to a different S3 bucket.
async.waterfall([
   function download(next) {
      // Download the image from S3 into a buffer.
      s3.getObject({
         Bucket: srcBucket,
         Key: srcKey
      },
   next);
   },
   function tranform(response, next) {
       gm(response.Body).size(function(err, size) {
       // Infer the scaling factor to avoid stretching the image unnaturally.
       var scalingFactor = Math.min(
       MAX_WIDTH / size.width,
       MAX_HEIGHT / size.height
    );
    var width = scalingFactor * size.width;
    var height = scalingFactor * size.height;

     // Transform the image buffer in memory.
     this.resize(width, height).toBuffer(imageType, function(err, buffer) {
        if (err) {
           next(err);
        } else {
        next(null, response.ContentType, buffer);
     }
   });
  });
},
function upload(contentType, data, next) {
    // Stream the transformed image to a different S3 bucket.
    s3.putObject({
       Bucket: dstBucket,
       Key: dstKey,
       Body: data,
       ContentType: contentType,
       ACL:'public-read' // Make publically available
    },
  next);
 }
], function (err) {
      if (err) {
         console.error(
            'Unable to resize ' + srcBucket + '/' + srcKey +
            ' and upload to ' + dstBucket + '/' + dstKey +
            ' due to an error: ' + err
         );
      } else {
        console.log(
           'Successfully resized ' + srcBucket + '/' + srcKey +
           ' and uploaded to ' + dstBucket + '/' + dstKey
        );
    }

     context.done();
   }
  );
};

We have implemented this in a hunting web & mobile application, BaseMap.

Pros and Cons of AWS Lambda

Pros

  • Rapid Development
    Serverless architecture can be rapidly developed. Server overloading/crash issues are mitigated. This enables engineers to focus on other important areas.
  • Minimal Operational Management
    The serverless platform segregates the infrastructure services and applications running on top of the OS and middleware.
    Also, the automatic scaling of ‘Functions as a Service’ (FaaS) reduces the computational and operations management overheads.
  • Reduces Operational Cost
    The basic merit of this technology is that you only pay for the time your function executes (on-demand billing).
  • Scalability & Availability
    AWS Lambda is designed to use replication and redundancy to provide high availability for the service and for the Lambda functions it operates. There are no maintenance windows or scheduled downtimes.
  • Language Agnostic
    AWS Lambda supports multiple languages using runtimes. As of December 2019, AWS Lambda supports:
    Node.js: Node.js 12, Node.js 10, Node.js 8.10
    Python: Python 3.8, Python 3.7, Python 3.6, Python 2.7
    Ruby: Ruby 2.5
    Java: Java 11, Java 8
    Go: Go 1.x
    .NET: .NET Core 2.1

It is possible to use other languages in Lambda using a custom runtime. The Lambda execution environment provides a runtime interface for invocation events and responses.

Cons

  • State
    This is a state-full service. A database is required to store data as this is a connection-oriented service.
  • DoS (Denial of Service)
    AWS Lambda limits concurrency. This may lead to deadlocks at times as the jobs in the ready queue may be temporarily sidelined.
  • Execution Duration
    AWS Lambda functions are aborted if they run for longer than 5 minutes or a pre-set time limit. Some tasks may remain unexecuted.
  • Monitoring and Debugging
    Lambda scales the batches with SQS jobs, picks them off the queue, processes tasks, and then shuts off when the task is completed.
    Monitoring and debugging is very basic in AWS lambda. This may work in a simple to moderate cases. More open APIs are probably the need of the hour to perform extensive monitoring and debugging.

 

AWS Lambda Resources.

Conclusion

AWS Lambda is a serverless architecture that enables custom development of an application using an event-driven computing platform. Managing and forwarding RPC calls to remote servers is avoided thereby optimizing computational time. Lambda function works seamlessly with Amazon S3.

AWS Lambda should be your first choice to create serverless apps if you are looking for low maintenance, low cost, ease of scalability, and are ready to build and manage your FaaS infrastructure.

  • AWS
  • AWS Lambda
  • AWS Lambda Use Case
Posted By

Sarang Sutar

Date

26 February 2020

Share
Stay updated with our Newsletter

Related Posts