4 min read

AWS Lambda is a new compute service introduced by AWS to run a piece of code in response to events. The source of these events can be AWS S3, AWS SNS, AWS Kinesis, AWS Cognito and User Application using AWS-SDK. The idea behind this is to create backend services that are cost effective and highly scaleable. If you believe in the Unix Philosophy and you build your applications as components, then AWS Lambda is a nice feature that you can make use of.

Some of Its Benefits

Cost-effective: AWS Lambdas are not always executing, they are triggered on certain events and have a maximum execution time of 60 seconds (it’s a lots of time to do many operations, but not all). There is zero wastage, and a maximum savings on resources used.

No hassle of maintaining infrastructure: Create Lambda and forget. There is no need to worry about scaling infrastructure as load increases. It will be all done automatically by AWS.

Integrations with other AWS service: The AWS Lambda function can be triggered in response to various events of other AWS Services. The following are services that can trigger a Lambda:

  • AWS S3
  • AWS SNS(Publish)
  • AWS Kinesis
  • AWS Cognito
  • Custom call using aws-sdk

Creating a Lambda function

First, login to your AWS account(create one if you haven’t got one). Under Compute Services click on the Lambda option. You will see a screen with a “Get Started Now” button. Click on it, and then you will be on a screen to write your first Lambda function. Choose a name for it that will describe it best. Give it a nice description and move on to the code. We can code it in one of the following two ways: Inline code or Upload a zip file.

Inline Code

Inline code will be very helpful for writing simple scripts like image editing. The AMI (Amazon Machine Image) that Lambda runs on comes with preinstalled Ghostscript and ImageMagick libraries and NodeJs packages like aws-sdk and imagemagick. Let’s create a Lambda that can list install packages on AMI and that runs Lambda.

  • I will name it ls-packages
  • The description will be list installed packages on AMI
  • For code entry, type Edit Code Inline
  • For the code template None, paste the below code in:
    var cp = require('child_process');
    
    exports.handler = function(event, context) {
       cp.exec('rpm -qa', function (err, stdout, stderr ) {
           if (err) {
               return context.fail(err);
           }
           console.log(stdout);
           context.succeed('Done');
       });
    };
  • Handler name handler, this will be the entry point function name. You can change it as you like.
  • Role, select Create new role Basic execution role. You will be prompted to create an IAM role with the required permission i.e. access to create logs. Press “Allow.”
  • For the Memory(MB), I am going to keep it low 128
  • Timeout(s), keep it default 3
  • Press Create Lambda function

You will see your first Lambda created and showing up in Lambda: Function list, select it if it is not already selected, and click on the Actions drop-down. On the top select the Edit/Test option. You will see your Lambda function in edit mode, ignore the left side Sample event section just client Invoke button on the right bottom, wait for a few seconds and you will see nice details in Execution result. The “Execution logs” is where you will find out the list of installed packages on the machine that you can utilize.

I wish there was a way to install custom packages, or at least have the latest version running of installed packages. I mean, look at ghostscript-8.70-19.23.amzn1.x86_64. It is an old version published in 2009.

Maybe AWS will add such features in the future. I certainly hope so.

Upload a zip file

You now have created something complicated that is included in multiple code files and NPM packages that are not available on Lambda AMI. No worries, just create a simple NodeJs app, install you packages in write up your code and we are good to deploy it. Few things that need to be take care of are: Zip node_modules folder along with code don’t exclude it while zipping your code. Steps will be the same as are of Inline Code online, but one addition is File name. File name will be path to entry file, so if you have lib dir in your code with index.js file then you can mention it as bin/index.js.

Monitoring

On the Lambda Dashboard you will see a nice graph of various events like Invocation Count, Invocation Duration, Invocation failures and Throttled invocations. You will also view the logs created by Lambda functions in AWS Cloud Watch(Administration & Security)

Conclusion

AWS Lambda is a unique, and very useful service. It can help us build nice scaleable backends for mobile applications. It can also help you to centralize many components that can be shared across applications that you are running on and off the AWS infrastructure.

About the author

Ankit Patial has a Masters in Computer Applications, and nine years of experience with custom APIs, web and desktop applications using .NET technologies, ROR and NodeJs. As a CTO with SimSaw Inc and Pink Hand Technologies, his job is to learn and and help his team to implement the best practices of using Cloud Computing and JavaScript technologies.


Subscribe to the weekly Packt Hub newsletter. We'll send you the results of our AI Now Survey, featuring data and insights from across the tech landscape.

LEAVE A REPLY

Please enter your comment!
Please enter your name here