Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Explain the working principle Aws lambda function

What is AWS Lambda
Explain the use cases of Lambda Function
When to use Lambda Function
How AWS Lambda Works
How it is possible to compute code by Aws lambda without any server
How it is possible without any server computes code without any server

What is AWS Lambda

1.AWS Lambda is a service which computes the code without any server. It is said to be serverless compute. The code is executed based on the response of events in AWS services such as adding/removing files in S3 bucket, updating Amazon DynamoDB tables, HTTP request from Amazon API Gateway etc.

Run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes

2.AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. It is designed to enable developers to run code without provisioning or managing servers
You organize your code into Lambda functions. The Lambda service runs your function only when needed and scales automatically. You only pay for the compute time that you consume—there is no charge when your code is not running. For more information, se

AWS Lambda is an Amazon serverless computing system that runs code and automatically manages the underlying computing resources like (EC2). It is an event-driven computing service. It lets a person automatically run code in response to many types of events, such as HTTP requests from the Amazon API gateway, table updates in Amazon DynamoDB, and state transitions. It also enables the person to extend to other AWS services with custom logic and even creates its own back-end services. For example, just write the code and then upload it as a .zip file or any container image. The service works by running code on high-availability computer infrastructure. It then performs all the administrative duties of that compute resource such as:

Image description

providing maintenance on the server and operating system, auto-scaling and managing the person’s capacity provisions, handling security patch deployment lambda extensions code, monitoring, logging, and concurrency function blueprints. AWS lambda is very helpful when you know how to write code but you don’t know how to provision the underlying infrastructure in AWS. AWS lambda will scale up the applications rapidly when there is sudden incoming traffic and scale down to zero when the incoming traffic is reduced.

Examples of AWS Lambdas:

  1. Process data from Amazon S3 buckets.
  2. Respond to HTTP requests.
  3. Build serverless applications.

Aws Lambda Foundation

You can configure your functions using the Lambda console, Lambda API, AWS CloudFormation or AWS SAM. You create code for the function and upload the code using a deployment package. Lambda invokes the function when an event occurs. Lambda runs multiple instances of your function in parallel, governed by concurrency and scaling limits.

When to use Lambda

Lambda is an ideal compute service for application scenarios that need to scale up rapidly, and scale down to zero when not in demand. For example, you can use Lambda for:

File processing: Use Amazon Simple Storage Service (Amazon S3) to trigger Lambda data processing in real time after an upload.

Stream processing: Use Lambda and Amazon Kinesis to process real-time streaming data for application activity tracking, transaction order processing, clickstream analysis, data cleansing, log filtering, i*ndexing, social media analysis*, Internet of Things (IoT) device data telemetry, and metering.

Web applications: Combine Lambda with other AWS services to build powerful web applications that automatically scale up and down and run in a highly available configuration across multiple data centers.

IoT backends: Build serverless backends using Lambda to handle web, mobile, IoT, and third-party API requests.

Mobile backends: Build backends using Lambda and Amazon API Gateway to authenticate and process API requests. Use AWS Amplify to easily integrate with your iOS, Android, Web, and React Native frontends.

How AWS Lambda Works

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second. You pay only for the compute time you consume - there is no charge when your code is not running.

How AWS Lambda Works?
The block diagram that explains the working of AWS Lambda in five easy steps is shown below −

Image description

Step 1 − Upload AWS lambda code in any of languages AWS lambda supports, that is NodeJS, Java, Python, C# and Go.

Step 2 − These are few AWS services on which AWS lambda can be triggered.

Step 3 − AWS Lambda which has the upload code and the event details on which the trigger has occurred. For example, event from Amazon S3, Amazon API Gateway, Dynamo dB, Amazon SNS, Amazon Kinesis, CloudFront, Amazon SES, CloudTrail, mobile app etc.

Step 4 − Executes AWS Lambda Code only when triggered by AWS services under the scenarios such as −

  1. User uploads files in S3 bucket
  2. http get/post endpoint URL is hit
  3. data is added/updated/deleted in dynamo dB tables
  4. push notification
  5. data streams collection
  6. hosting of website
  7. email sending
  8. mobile app, etc.

.
Step 5 − Remember that AWS charges only when the AWS lambda code executes, and not otherwise.

Advantages of using AWS Lambda
AWS Lambda offers multiple benefits when you are working on it. This section discusses them in detail −

Ease of working with code
AWS Lambda gives you the infrastructure to upload your code. It takes care of maintaining the code and triggers the code whenever the required event happens. It allows you to choose the memory and the timeout required for the code.

AWS Lambda can also execute parallel requests as per the event triggers.

Log Provision
AWS Lambda gives the details of number of times a code was executed and time taken for execution, the memory consumed etc. AWS CloudWatch collects all the logs, which helps in understanding the execution flow and in the debugging of the code.

Billing based on Usage
AWS Lambda billing is done on memory usage, request made and the execution, which is billed in increments of minimum 100ms. So for a 500ms execution, the billing will be after every 100ms. If you specify your AWS lambda code to be executed in 500ms and the time taken to execute is just 200ms, AWS will bill you only for the time taken, that is 200ms of execution instead of 500ms. AWS always charges for the execution time used. You need not pay if the function is not executed.

Multi Language Support
AWS Lambda supports popular languages such as Node. js, Python, Java, C# and Go. These are widely used languages and any developer will find it easy to write code for AWS Lambda.

Ease of code authoring and deploying
There are many options available for Lambda for authoring and deploying code. For writing your code, you can use AWS online editor, Visual Studio IDE, or Eclipse IDE. It also has support for serverless framework which makes writing and deploying of AWS Lambda code easy. Besides AWS console, we have AWS-cli to create and deploy code.

Other features
You can use AWS Lambda for free by getting a login to AWS free tier. It gives you service for free for 1 year. Take a look at the free services offered by AWS free tier.

Disadvantages of using AWS Lambda
In spite of many advantages, AWS Lambda possesses the following disadvantages −

It is not suitable for small projects.

You need to carefully analyze your code and decide the memory and timeout. Incase if your function needs more time than what is allocated, it will get terminated as per the timeout specified on it and the code will not be fully executed.

Since AWS Lambda relies completely on AWS for the infrastructure, you cannot install anything additional software if your code demands it.

Events that Trigger AWS Lambda
The events can trigger AWS Lambda are as follows −

  1. Entry into a S3 object
  2. Insertion, updation and deletion of data in Dynamo DB table
  3. Push notifications from SNS
  4. GET/POST calls to API Gateway
  5. Headers modification at viewer or origin request/response in CloudFront
  6. Log entries in AWS Kinesis data stream
  7. Log history in CloudTrail
  8. Use Cases of AWS Lambda

explain-events-that-trigger-the-aws-lambda
AWS Lambda is a compute service mainly used to run background processes. It can trigger when used with other AWS services. The list of AWS services where we can use AWS Lambda is given below −

S3 Object and AWS Lambda
Amazon S3 passes the event details to AWS Lambda when there is any file upload in S3. The details of the file upload or deletion of file or moving of file is passed to the AWS Lambda. The code in AWS Lambda can take the necessary step for when it receives the event details. For Example creating thumbnail of the image inserted into S3.

DynamoDB and AWS Lambda
DynamoDB can trigger AWS Lambda when there is data added, updated and deleted in the table. AWS Lambda event has all the details of the AWS DynamoDB table about the insert /update or delete.

API Gateway and AWS Lambda
API Gateway can trigger AWS Lambda on GET/POST methods. We can c*reate a form and share details with API Gateway endpoint* and use it with AWS Lambda for further processing, for Example, making an entry of the data in DynamoDB table.

SNS and AWS Lambda
SNS is used for push notification, sending SMS etc. We can trigger AWS lambda when there is any push notification happening in SNS. We can also send SMS to the phone number from AWS Lambda when it receives the trigger.

Scheduled Events and AWS Lambda
Scheduled Events can be used for cron jobs. It can trigger AWS Lambda to carry out the task at regular time pattern.

CloudTrail and AWS Lambda
CloudTrail can be helpful in monitoring the logs on the account. We can use AWS Lambda to further process the CloudTrail logs .

Kinesis and AWS Lambda
Kinesis is used to capture/store real time tracking data coming from website clicks, logs, social media feeds and a trigger to AWS Lambda can do additional processing on this logs.

CloudFront and Lambda@Edge
CloudFront is a content delivery network where you can host your website and Lambda@Edge can be used to process the headers coming from viewer request, origin request, origin response and viewer response. The headers modification includes tasks such as modifying cookie data, URL rewrite, used for AB testing to change the response send to the user back, adding extra headers info for security purpose etc.

How it is possible to compute code by Aws lambda without any server

The term "serverless" can be a bit misleading. While it's true that with AWS Lambda, you don't have to manage servers explicitly, there are still servers involved in the background. The key distinction lies in the fact that, as a developer, you are abstracted from the underlying infrastructure management.

Here's a more detailed explanation:

Abstraction of Infrastructure:

When you deploy a function on AWS Lambda, you provide the function code along with its dependencies.
AWS Lambda abstracts the underlying infrastructure, including servers, operating systems, and runtime environments.
As a developer, you don't need to provision or manage these servers. AWS takes care of server provisioning, maintenance, and scaling.
Containerization:

AWS Lambda uses containerization to encapsulate and run your function code.
Each function execution runs in an isolated container. This container includes the necessary runtime, libraries, and dependencies for your function.
Event-Driven Model:

AWS Lambda operates on an event-driven model. Functions are triggered by events such as changes in data (e.g., S3 updates), HTTP requests (via API Gateway), or other supported event sources.
When an event occurs, AWS Lambda automatically spins up an environment (container) to execute the associated function.
Statelessness:

AWS Lambda functions are designed to be stateless. There is no persistent storage attached to a specific function instance.
The statelessness allows for easy scaling and parallel execution of multiple function instances.
Dynamic Resource Allocation:

AWS Lambda dynamically allocates compute resources based on the requirements specified for each function.
You define the amount of memory allocated to your function, and AWS Lambda allocates CPU, network bandwidth, and disk I/O resources proportionally.
Server Management Abstraction:

AWS Lambda abstracts server management tasks such as patching, updating, and securing the underlying infrastructure.
Developers don't need to worry about server provisioning, maintenance, or dealing with issues related to traditional server management.
Scalability:

AWS Lambda scales automatically in response to the rate of incoming events.
If there's a surge in requests, AWS Lambda can quickly scale out by creating additional instances of the function to handle the load.
While AWS Lambda doesn't eliminate servers entirely, it significantly simplifies the development and deployment process by abstracting server management tasks, allowing developers to focus on writing code and building applications.

How it is possible without any server computes code without any server

AWS Lambda abstracts the underlying infrastructure through a combination of containerization, event-driven architecture, and dynamic resource allocation. Let's break down the key components and concepts that enable this abstraction:

Containerization:

AWS Lambda uses containerization technology to encapsulate and run functions in isolated environments.
Containers are lightweight, standalone, and executable packages that include everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings.
Event-Driven Model:

AWS Lambda operates on an event-driven model, where functions are triggered by events such as changes in data, HTTP requests, database updates, or other supported event sources.
When an event occurs, AWS Lambda automatically spins up an environment (container) to execute the associated function.
Dynamic Resource Allocation:

Developers specify the amount of memory allocated to a function (e.g., 128 MB, 256 MB, etc.).
AWS Lambda dynamically allocates CPU power, network bandwidth, and disk I/O based on the chosen memory size.
This dynamic allocation allows for efficient resource utilization and scaling.
Isolation and Statelessness:

Each function execution runs in an isolated container, providing security and isolation from other functions.
AWS Lambda functions are designed to be stateless, meaning there is no persistent storage attached to a specific function instance.
Statelessness enables easy scaling and parallel execution without concerns about maintaining state between different instances.
Automatic Scaling:

AWS Lambda scales automatically in response to the rate of incoming events.
If there's a surge in requests, AWS Lambda can quickly scale out by creating additional instances of the function to handle the load.
Scaling is handled seamlessly by AWS Lambda, and developers don't need to manually provision or manage servers.
Managed Infrastructure:

AWS Lambda abstracts the management of underlying infrastructure, such as servers and operating systems.
AWS takes care of tasks like server provisioning, patching, updating, and security.
Developers are relieved of the burden of traditional server management, allowing them to focus on writing code and building applications.
In summary, AWS Lambda leverages containerization, an event-driven model, dynamic resource allocation, isolation, and automatic scaling to abstract the complexities of underlying infrastructure. This abstraction simplifies the development and deployment process, enabling developers to build scalable and efficient serverless applications.

Lambda function
Aws lambda latest
Aws lambda
aws-lambda
aws-lambda-with-python-a-complete-getting-started-guide
aws_lambda_overview
Aws Lambda Function
Refrences4
aws-lambda-architecture/
aws-lambda-architecture/

aws-lambda-architecture/

Top comments (0)