Learn Serverless with few simple steps— AWS Lambda (Part 1)

Krunal Shah

Apr 18, 2018 | 3 min read

Learn Serverless with few simple steps AWS Lambda

What is Lambda?

AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. You can use AWS Lambda to extend other AWS services with custom logic, or create your own back-end services that operate at AWS scale, performance, and security. AWS Lambda can automatically run code in response to multiple events, such as HTTP requests via Amazon API Gateway, modifications to objects in Amazon S3 buckets, table updates in Amazon DynamoDB, and state transitions in AWS Step Functions.

Lets create our first Lambda -> “Hello from Lambda”

It is very simple to create lambda function. Just login into AWS lambda console and click on Create Function button on screen.

As you can see in the above image, you need to give name to lambda function as well as select the run time environment. Lambda supports node 4,6 and 8.

Lambda needs role for execution, this need to be provided over here or you select from any existing role.

exports.handler = function(event, context, callback) {
   callback(null, "Hello from Lambda");
}

The above code snippet is the simple hello world program

You can test the lambda by clicking on Test button on top right on AWS lambda console.

Lets create our second Lambda -> “Pass parameter to Lambda and access it”

You use parameters in lambda function as per your need.

{
 "params" : {
     "path" : {
         "name" : "Kruti"
     }
 } 
}

The event.json can be used as below.

exports.handler = (event, context, callback) => {
    // TODO implement
    callback(null, 'Hello ! ' + event.params.path.name);
};

Handle success and failure in Lambda ->

Lambda handles success and failure in two ways:

  1. Callback
  2. Context

Handling with Callback

exports.handler = function(event, context, callback) {
  if(event.key == 'Hello'){
      callback(null, 'Success')
  } else {
      callback('Failure')
  }
}

Success Case:

Failure Case:

Handling with Context

exports.handler = function(event, context, callback) {
  if(event.key == 'Hello'){
      context.succeed({ message : 'Success'})
  } else {
      context.fail(JSON.stringify({ message : 'Failure'}))
  }
}

Success Case:

Failure Case:

Ways to upload code in a lambda ->

There are three ways in which you can code or upload in lambda.

  1. Inline Code
  2. Upload Zip (Upto 10MB)
  3. Upload from S3 bucket

Inline code is convenient when your lambda functional is small and you don’t need any dependencies.

While if you need any dependencies you must opt for Upload zip, where you can use package.json and node_modules and upload it to AWS lambda function. But here is limitation, it only support till 10MB.

In certain scenario, the function is size goes beyond 10MB due to more dependencies on node_module. In that case, upload you zip on S3 bucket and pass it’s URL.

Stay tuned for the part two of the blog “Learn Serverless with few simple step”.

Happy coding !!!

· · · ·

Third Rock Techkno is a leading IT services company. We are a top-ranked web, voice and mobile app development company with over 10 years of experience. Client success forms the core of our value system.

We have expertise in the latest technologies including angular, react native, iOs, Android and more. Third Rock Techkno has developed smart, scalable and innovative solutions for clients across a host of industries.

Our team of dedicated developers combine their knowledge and skills to develop and deliver web and mobile apps that boost business and increase output for our clients.