0% found this document useful (0 votes)
6 views

lambda

AWS Lambda is a serverless computing service that allows users to run code in response to various events without managing servers. It features auto-scaling, pay-per-use pricing, and integration with other AWS services, while also having limitations such as execution time limits and vendor lock-in. Lambda functions utilize a programming model that includes functions, triggers, events, and deployment packages, which can be in the form of .zip files or container images.

Uploaded by

subham joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

lambda

AWS Lambda is a serverless computing service that allows users to run code in response to various events without managing servers. It features auto-scaling, pay-per-use pricing, and integration with other AWS services, while also having limitations such as execution time limits and vendor lock-in. Lambda functions utilize a programming model that includes functions, triggers, events, and deployment packages, which can be in the form of .zip files or container images.

Uploaded by

subham joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

AWS (Amazon Web Services) Lambda Function

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.
What Are Lambdas Functions?

AWS lambda are server-less compute functions are fully managed by the
AWS where developers can run there code without worrying about servers.
AWS lambda functions will allow you to run the code with out provisioning or
managing servers.
Once you upload the source code file into AWS lambda in the form of ZIP file
then AWS lambda will automatically run the code without you provision the
servers and also it will automatically scaling your functions up or down based
on demand.
Features Of AWS (Amazon Web Services) Lambda Functions
1. AutoScaling and High Availability: AWS lambda will make sure that
your application was highly available to the end users when there is
sudden incoming traffic. High availability can be achieved by scaling the
application.
2. Serverless Execution: There is no need for provisioning the servers
manually in AWS. AWS lambda will provision the underlying
infrastructure based on the triggers you are mentioned whenever a new
file uploaded to a particular then AWS lambda will automatically trigger
and takes care of the infrastructure.
3. Pay-per-use-pricing: AWS will charge you only for the time that time
compute engine was active. AWS bills you based on the time taken to
execute the code.
4. Supports different programming languages
5. Integrates with other AWS Services: AWS lambda can be integrated
with different AWS services like the following : API Gateway,
DynamoDB, S3, SNS, SQS.
6. Versioning and Deployment: AWS lambda function will maintain the
different kinds of versions of the code by which you can change
between the versions without any disruptions y based on the application
performances.
7. Security and Identity Management: AWS lambda will leverage AWS
Identity and Access Management (IAM) to control the access to the
functions which are built by using lambda. It is also possible to define
fine-grained permissions and policies to secure your functions and
ensure that only authorised entities can invoke them.

Advantages of AWS (Amazon Web Services) Lambda Function


1. Zero Server Management: Since AWS Lambda automatically runs the
user’s code, there’s no need for the user to manage the server. Simply
write the code and upload it to Lambda.
2. Scalability: AWS Lambda runs code in response to each trigger, so the
user’s application is automatically scaled. The code also runs in parallel
processes, each triggered individually, so scaling is done precisely with
the size of the workload.
3. Event-Driven Architecture: AWS Lambda function can be triggered
based on the events happing in the aws other service like when the file
or video is added to the S3 bucket you can trigger the AWS Lambda
function.
4. Automatic High Availability: When there is high demand or high
incoming traffic aws lambda function will automatically scale the
servers.
5. Affordable: With AWS Lambda, one doesn’t pay anything when the
code isn’t running. The user has to only be charged for every 100ms of
code execution and the number of times his code is actually triggered.

Disadvantages of AWS (Amazon Web Services) Lambda Function


1. Latency while starting: While aws lambda is going to be activated
after a long gap it will take some time to initialize the service which is
required to deploy the application at that time end users will face
latency issues.
2. Limited control of infrastructure: Behalf of your lambda function is
going to take of underlying infrastructure so you will have very limited
control over undelaying infrastructure.
3. Time Limit: AWS Lambda enforces a maximum execution time limit for
functions, which is currently set to 900 seconds (15 minutes). If your
function exceeds this time limit, it will be forcibly terminated.
4. Vendor Lock-In: If you want to execute the lambda function then you
need the support of any cloud provider as here we are using AWS
because it is widely used in the market.
5. Limited Computing and Memory Options: Limited configuration is
there on the memory and CPU configuration. The predefined memory
configuration was 120 MB to 120 GB and memory configuration
determines the corresponding CPU power.

Lambda provides a programming model that is common to all of the runtimes.


The programming model defines the interface between your code and the
Lambda system. You tell Lambda the entry point to your function by defining
a handler in the function configuration. The runtime passes in objects to the
handler that contain the invocation event and the context, such as the
function name and request ID.

When the handler finishes processing the first event, the runtime sends it
another. The function's class stays in memory, so clients and variables that
are declared outside of the handler method in initialization code can be
reused. To save processing time on subsequent events, create reusable
resources like AWS SDK clients during initialization. Once initialized, each
instance of your function can process thousands of requests.

Your function also has access to local storage in the /tmp directory. The
directory content remains when the execution environment is frozen,
providing a transient cache that can be used for multiple invocations.

Lambda concepts -
Lambda runs instances of your function to process events. You can invoke
your function directly using the Lambda API, or you can configure an AWS
service or resource to invoke your function.

Function

A function is a resource that you can invoke to run your code in Lambda. A
function has code to process the events that you pass into the function or that
other AWS services send to the function.

Trigger
A trigger is a resource or configuration that invokes a Lambda function.
Triggers include AWS services that you can configure to invoke a function
and event source mappings. An event source mapping is a resource in
Lambda that reads items from a stream or queue and invokes a function.

Event

An event is a JSON-formatted document that contains data for a Lambda


function to process. The runtime converts the event to an object and passes it
to your function code. When you invoke a function, you determine the
structure and contents of the event.

Example custom event – weather data

{
"TemperatureK": 281,
"WindKmh": -3,
"HumidityPct": 0.55,
"PressureHPa": 1020
}
When an AWS service invokes your function, the service defines the shape of
the event.

Example service event – Amazon SNS notification

{
"Records": [
{
"Sns": {
"Timestamp": "2019-01-02T12:45:07.000Z",
"Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/
1+82j...65r==",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"Message": "Hello from SNS!",

Example kinesis event - Kinesis Data Stream

[
{
"kinesis":
{
"kinesisSchemaVersion": "1.0",
"partitionKey": "123",
"sequenceNumber":
"49650125799947206369496608154356975047631630989885702146",
"data": "Testing2",
"approximateArrivalTimestamp": 1710401993.757
},
"eventSource": "aws:kinesis",
"eventVersion": "1.0",
"eventID":
"shardId-000000000000:496501257999472063694966081543569750476316
30989885702146",
"eventName": "aws:kinesis:record",
"invokeIdentityArn": "arn:aws:iam::343550350117:role/prd2413.maps.us-
east-1.nonprod.opensearch-curator",
"awsRegion": "us-east-1",
"eventSourceARN": "arn:aws:kinesis:us-east-1:343550350117:stream/
PRD192-dev-geoJSON-lambda-message"
}
]

Execution environment

An execution environment provides a secure and isolated runtime


environment for your Lambda function. An execution environment manages
the processes and resources that are required to run the function. The
execution environment provides lifecycle support for the function and for any
extensions associated with your function.

Instruction set architecture

The instruction set architecture determines the type of computer processor


that Lambda uses to run the function. Lambda provides a choice of instruction
set architectures:
• arm64 – 64-bit ARM architecture, for the AWS Graviton2 processor.
• x86_64 – 64-bit x86 architecture, for x86-based processors.

Deployment package

You deploy your Lambda function code using a deployment package.


Lambda supports two types of deployment packages:

• A .zip file archive that contains your function code and its
dependencies. Lambda provides the operating system and runtime for
your function.
• A container image- You add your function code and dependencies to
the image. You must also include the operating system and a Lambda
runtime.

Runtime

The runtime provides a language-specific environment that runs in an


execution environment. The runtime relays invocation events, context
information, and responses between Lambda and the function. You can use
runtimes that Lambda provides, or build your own. If you package your code
as a .zip file archive, you must configure your function to use a runtime that
matches your programming language. For a container image, you include the
runtime when you build the image.

Layer

A Lambda layer is a .zip file archive that can contain additional code or other
content. A layer can contain libraries, a custom runtime, data, or configuration
files.

Layers provide a convenient way to package libraries and other


dependencies that you can use with your Lambda functions. Using layers
reduces the size of uploaded deployment archives and makes it faster to
deploy your code. Layers also promote code sharing and separation of
responsibilities so that you can iterate faster on writing business logic.
You can include up to five layers per function. When you include a layer in a
function, the contents are extracted to the /opt directory in the execution
environment.

By default, the layers that you create are private to your AWS account. You
can choose to share a layer with other accounts or to make the layer public. If
your functions consume a layer that a different account published, your
functions can continue to use the layer version after it has been deleted, or
after your permission to access the layer is revoked. However, you cannot
create a new function or update functions using a deleted layer version.

Functions deployed as a container image do not use layers. Instead, you


package your preferred runtime, libraries, and other dependencies into the
container image when you build the image.

Extension

Lambda extensions enable you to augment your functions. For example, you
can use extensions to integrate your functions with your preferred monitoring,
observability, security, and governance tools.

An internal extension runs in the runtime process and shares the same
lifecycle as the runtime. An external extension runs as a separate process in
the execution environment. The external extension is initialised before the
function is invoked, runs in parallel with the function's runtime, and continues
to run after the function invocation is complete.

Concurrency

Concurrency is the number of requests that your function is serving at any


given time. When your function is invoked, Lambda provisions an instance of
it to process the event. When the function code finishes running, it can handle
another request. If the function is invoked again while a request is still being
processed, another instance is provisioned, increasing the function's
concurrency.

Concurrency is subject to quotas at the AWS Region level. You can configure
individual functions to limit their concurrency, or to enable them to reach a
specific level of concurrency.

Qualifier
When you invoke or view a function, you can include a qualifier to specify a
version or alias. A version is an immutable snapshot of a function's code and
configuration that has a numerical qualifier. For example, my-function:1.
An alias is a pointer to a version that you can update to map to a different
version, or split traffic between two versions. For example, my-function:BLUE.
You can use versions and aliases together to provide a stable interface for
clients to invoke your function.

Destination

A destination is an AWS resource where Lambda can send events from an


asynchronous invocation. You can configure a destination for events that fail
processing. Some services also support a destination for events that are
successfully processed.

How it works?

Following are the three major components of (Amazon Web Services)


Lambda Functions.
1. Function.
2. Configuration.
3. Event Source.

Lambda invokes your function in an execution environment, which provides a


secure and isolated runtime environment. The execution environment
manages the resources required to run your function. The execution
environment also provides lifecycle support for the function's runtime and any
external extensions associated with your function.

The function's runtime communicates with Lambda using the Runtime API.
Extensions communicate with Lambda using the Extensions API Extensions
API. Extensions can also receive log messages and other telemetry from the
function by using the Telemetry API.

Lambda execution environment lifecycle

Each phase starts with an event that Lambda sends to the runtime and to all
registered extensions. The runtime and each extension indicate completion
by sending a Next API request. Lambda freezes the execution environment
when the runtime and each extension have completed and there are no
pending events.

Lambda deployment packages:

Your AWS Lambda function's code consists of scripts or compiled programs


and their dependencies. You use a deployment package to deploy your
function code to Lambda. Lambda supports two types of deployment
packages: container images and .zip file archives.

Topics-

• Container Images
• .zip file archives
• Layers
• Using other AWS services to build a deployment package
Container images

A container image includes the base operating system, the runtime, Lambda
extensions, your application code and its dependencies. You can also add
static data, such as machine learning models, into the image.
Lambda provides a set of open-source base images that you can use to build
your container image. To create and test container images, you can use the
AWS Serverless Application Model (AWS SAM) command line interface (CLI)
or native container tools such as the Docker CLI.

You upload your container images to Amazon Elastic Container Registry


(Amazon ECR), a managed AWS container image registry service. To deploy
the image to your function, you specify the Amazon ECR image URL using
the Lambda console, the Lambda API, command line tools, or the AWS
SDKs.

.zip file archives

A .zip file archive includes your application code and its dependencies. When
you author functions using the Lambda console or a toolkit, Lambda
automatically creates a .zip file archive of your code.

When you create functions with the Lambda API, command line tools, or the
AWS SDKs, you must create a deployment package. You also must create a
deployment package if your function uses a compiled language, or to add
dependencies to your function. To deploy your function's code, you upload the
deployment package from Amazon Simple Storage Service (Amazon S3) or
your local machine.

You can upload a .zip file as your deployment package using the Lambda
console, AWS Command Line Interface (AWS CLI), or to an Amazon Simple
Storage Service (Amazon S3) bucket.

Using the AWS CLI

You can upload a .zip file as your deployment package using the AWS
Command Line Interface (AWS CLI).

Using Amazon S3

You can upload a .zip file as your deployment package using Amazon Simple
Storage Service (Amazon S3).

Layers

If you deploy your function code using a .zip file archive, you can use Lambda
layers as a distribution mechanism for libraries, custom runtimes, and other
function dependencies. Layers enable you to manage your in-development
function code independently from the unchanging code and resources that it
uses. You can configure your function to use layers that you create, layers
that AWS provides, or layers from other AWS customers.

You do not use layers with container images. Instead, you package your
preferred runtime, libraries, and other dependencies into the container image
when you build the image.

Using other AWS services to build a deployment package

The following section describes other AWS services you can use to package
dependencies for your Lambda function.

Deployment packages with C or C++ libraries

If your deployment package contains native libraries, you can build the
deployment package with AWS Serverless Application Model (AWS SAM).
You can use the AWS SAM CLI sam build command with the --use-
container to create your deployment package. This option builds a
deployment package inside a Docker image that is compatible with the
Lambda execution environment.

Deployment packages over 50 MB

If your deployment package is larger than 50 MB, upload your function code
and dependencies to an Amazon S3 bucket.

You can create a deployment package and upload the .zip file to your
Amazon S3 bucket in the AWS Region where you want to create a Lambda
function. When you create your Lambda function, specify the S3 bucket name
and object key name on the Lambda console, or using the AWS CLI.

You might also like