Professional Documents
Culture Documents
AWS Lambda
AWS Lambda
When using Lambda, you are responsible only for your code. Lambda manages the
compute fleet that offers a balance of memory, CPU, network, and other resources
to run your code. Because Lambda manages these resources, you cannot log in to
compute instances or customize the operating system on provided runtimes.
Lambda performs operational and administrative activities on your behalf, including
managing capacity, monitoring, and logging your Lambda functions.
When should you use Lambda?
If you need to manage your own compute resources, AWS has other compute services to meet
your needs. For example:
● Amazon Elastic Compute Cloud (Amazon EC2) offers a wide range of EC2 instance types
to choose from. It lets you customize operating systems, network and security settings,
and the entire software stack. You are responsible for provisioning capacity, monitoring
fleet health and performance, and using Availability Zones for fault tolerance.
● AWS Elastic Beanstalk enables you to deploy and scale applications onto Amazon EC2.
You retain ownership and full control over the underlying EC2 instances.
Lambda Functions
Let's start by defining what a Lambda function is. A Lambda function contains your code that you have
written to perform the required task plus any dependencies for that code. In addition to this, the function
also includes a number of configurational details, which are defined when a Lambda function is created.
● The required resources. The required resources provides just that, the resources that are required to
carry out the execution of the code, however, the only input we have is to specify the amount of RAM
required. The compute power is then determined by AWS Lambda using a ratio related to the
amount of RAM specified.
● Maximum execution timeout, this specifies the maximum amount of time the Lambda function will
run before it's terminated.
● IAM role, this grants the Lambda the appropriate permissions to carry out the execution of the code.
● Handler name, this refers to the method in your code where the execution of the function starts.
When the function is triggered, any event information is passed as a parameter to the handler
method.
● There is another key element of AWS Lambda, and that is defining the trigger that invokes the AWS
Lambda function itself.
Lambda Triggers
● 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.
Lambda Events
● 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.
●
Lambda Runtimes
● 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.
Lambda Layers
● 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
Lambda 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.
Lambda 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.
Lambda Concurrency
The first time you invoke your function, AWS Lambda creates an instance of the
function and runs its handler method to process the event. When the function
returns a response, it stays active and waits to process additional events. If you
invoke the function again while the first event is being processed, Lambda
initializes another instance, and the function processes the two events
concurrently.
As more events come in, Lambda routes them to available instances and creates
new instances as needed. When the number of requests decreases, Lambda
stops unused instances to free up scaling capacity for other functions.
Lambda Concurrency
The first time you invoke your function, AWS Lambda creates an instance of the
function and runs its handler method to process the event. When the function
returns a response, it stays active and waits to process additional events. If you
invoke the function again while the first event is being processed, Lambda
initializes another instance, and the function processes the two events
concurrently.
As more events come in, Lambda routes them to available instances and creates
new instances as needed. When the number of requests decreases, Lambda
stops unused instances to free up scaling capacity for other functions.
Lambda Concurrency
After the initial burst, your functions' concurrency can scale by an additional 500 instances each minute.
This continues until there are enough instances to serve all requests, or until a concurrency limit is
reached. When requests come in faster than your function can scale, or when your function is at
maximum concurrency, additional requests fail with a throttling error (429 status code).
The following example shows a function processing a spike in traffic. As invocations increase
exponentially, the function scales up. It initializes a new instance for any request that can't be routed to an
available instance. When the burst concurrency limit is reached, the function starts to scale linearly. If this
isn't enough concurrency to serve all requests, additional requests are throttled and should be retried.
Lambda Concurrency
Lambda Concurrency
The function continues to scale until the account's concurrency limit for the
function's Region is reached. The function catches up to demand, requests
subside, and unused instances of the function are stopped after being idle for
some time. Unused instances are frozen while they're waiting for requests and
don't incur any charges.
When your function scales up, the first request served by each instance is
impacted by the time it takes to load and initialize your code. If your initialization
code takes a long time, the impact on average and percentile latency can be
significant. To enable your function to scale without fluctuations in latency, use
provisioned concurrency. The following example shows a function with
provisioned concurrency processing a spike in traffic.
Lambda Concurrency
Lambda Concurrency