lambda
lambda
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.
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
{
"TemperatureK": 281,
"WindKmh": -3,
"HumidityPct": 0.55,
"PressureHPa": 1020
}
When an AWS service invokes your function, the service defines the shape of
the event.
{
"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!",
…
[
{
"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
Deployment package
• 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
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.
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.
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 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
How it works?
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.
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.
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.
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.
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.
The following section describes other AWS services you can use to package
dependencies for your Lambda function.
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.
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.