Chapter 1-Lambda Introduction

AWS Lambda

Before learning to AWS Lambda function, we should know few basics about serverless. Let’s understand serverless computing then we will learn about Lambda function.

Q: What is serverless computing?

Serverless computing is a cloud-computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources. It allows us to build and run applications and services without thinking about servers. With serverless computing, our application still runs on servers, but all the server management is done by AWS. That is why it became important for all projects or companies. AWS Lambda is core of this serverless computing. We can run our code without servers.

Q: What is AWS Lambda?

AWS Lambda is a compute service where we can upload our code and create a Lambda function. AWS Lambda takes care of provisioning and managing the servers that we use to run the code. We don’t have to worry about operating systems, patching, scaling, etc.

Basically, we can run code without provisioning or managing servers with help of AWS Lambda. We must pay only for the compute time. There is no charge when our code is not running. With Lambda, we can run code for virtually any type of application or backend service – all with zero administration. Just upload your code and Lambda takes care of everything.

We can use Lambda in the following ways;

  • As an event-driven compute service where AWS Lambda runs our code in response to events.
  • These events could be changes to data in an Amazon S3 bucket or DynamoDB table.
  • As a compute service to run your code in response to HTTP requests using Amazon API Gateway or API calls made using AWS SDKs.

Q: When should I use AWS Lambda versus Amazon EC2?

Amazon EC2 offers flexibility, with a wide range of instance types and the option to customize the operating system, network and security settings, and the entire software stack, allowing you to easily move existing applications to the cloud.

With Amazon EC2 you are responsible for provisioning capacity, monitoring fleet health and performance, and designing for fault tolerance and scalability. 

Where AWS Lambda makes it easy to execute code in response to events, such as changes to Amazon S3 buckets, updates to an Amazon DynamoDB table, or custom events generated by your applications or devices.

With Lambda we do not have to provision our instances; Lambda performs all the operational and administrative activities on our behalf, including capacity provisioning, monitoring fleet health, applying security patches to the underlying compute resources, deploying your code, running a web service front end, and monitoring and logging your code.

What Languages Does Lambda Support?

Node.js

Java

python

GO

PowerShell

Why Is Lambda good?

NO SERVERS!

Continuous Scaling

As we came to know that AWS Lambda is a serverless computing platform that allows us to create a small function.

After creating function, we can configure the function in the AWS console. Once it is configured then can execute code without the need to provision servers and can pay only for the resources used during the execution. As many organizations move towards implementing serverless architectures, AWS Lambda will play big role in future.

To understand how to write a Lambda function, we should understand what goes into one.

A Lambda function has a few requirements. The first requirement we need to satisfy is to provide a handler. The handler is the entry point for the Lambda. A Lambda function accepts JSON-formatted input and will usually return the same.

The second requirement is that we need to specify the runtime environment for the Lambda. The runtime will usually correlate directly with the language that we have selected to write function.

The final requirement is a trigger. We can configure a Lambda request in response to an event, such as a new file uploaded to S3, or a similar AWS event. You can also configure the Lambda to respond to requests to AWS API Gateway, or based on a timer triggered by AWS Cloudwatch.

Use Case: We will understand Lambda concept through simple use case.

Let’s pass two numbers into the function, and have it return the sum, product, difference, and quotient of the two numbers.

Input

{

   “Number1”: 20,

   “Number2”: 10

}

 Response:

{
   "Number1": 20,
   "Number2": 10,
   "Sum": 30,
   "Product": 200,
   "Difference": -10,
   "Quotient": 1
}

Writing Your Lambda function with Python

Step 1: Log in to your AWS Account, and navigate to the Lambda console.

Step 2: Click on Create function.

Step 3: We’ll be creating a Lambda from scratch, so select the Author from scratch option.

Enter name for your Lambda function, select a Python runtime and define a role for your Lambda to use.

Step 4: click on the Create function button. Next we can see below screen:

Click on the Select a test event drop-down and choose Configure test events.

Click on Test  button.

Step 5: Amazon provides collection of test templates. Select Create new test event and provide Event name as our EventName i.e. validatetwonumber.

Now We will modify the Hello World template with data of our own.

click on Create to create the new test event.

Step 6: We can modify code as per our need. So I have changed it as below:

import json

def lambda_handler(event, context):

   number1 = event[‘Number1’]

   number2 = event[‘Number2’]

   sum = number1 + number2

   product = number1 * number2

   difference = abs(number1 – number2)

   quotient = number1 / number2

   return {

       “Number1”: number1,

       “Number2”: number2,

       “Sum”: sum,

       “Product”: product,

       “Difference”: difference,

       “Quotient”: quotient

   }

Same I have kept in lambda function.

Save it.

Now click on Test Button to execute.


After execution we can see Results as below:

This is an example through which we can understand Lambda function and its uses.

************************************************************************

Important Points:

• Lambda scales out (not up) automatically

• Lambda functions are independent, 1 event = 1 function

• Lambda is serverless

Lambda functions can trigger other lambda functions, 1 event can = x

functions if functions trigger other functions