AWS Lambda
Last updated
Last updated
Lambda is the so called as "server-less architecture" of AWS. It is different from the previous two as it is event driven; so the code uploaded in Lambda, is not given an execution environment to run code, until the defined event is triggered, and in this process, the user does not get to either provision or manage servers, and hence user only pay for the time used to consume compute resources. Due to this, there is a limitation, that whatever code is uploaded in Lambda, should complete execution within 5 minutes
Here, lot of application or back‑end service is supported and as there is no scope for administration, it integrates with many AWS services.
One of the most important features is that it is highly scalable.
Pricing changes with memory. An AWS user can consume memory from 128 to 3008 MB allocated for functions that are running on AWS Lambda.
As explained in IAM, Roles are used for one AWS service to communicate with other service
AWS Lambda supports versioning (max versions are 5) and each version is immutable, i.e., once published, they cannot be changed and if changes are required, publish a new version, so in all, it can look like this:
LambdaFunctionName
LambdaFunctionName:1 <- Immutable
LamdaFunctionName:2 <- Immutable
Execution Environments are secure and isolated runtime environments
The lifecycle can be defined as shown below:
Extension INIT - In the Extension Init phase the system starts all extensions to let them apply their logic to the Lambda
Runtime INIT - The runtime is the combination of an operating system, programming language and a combination of software libraries required to invoke the Lambda function
AWS Lambda supports parallel invocations:
The characteristics of Lambda Execution environment are as follows:
Read Only File System
Memory based on Lambda configuration during setup
512 MB of disk space in /tmp directory
No inbound connections
Only TCP/IP Outbound connections are allowed
Max timeout of 15 minutes
The solution to above problem can be diagrammatically summarized as: Use Asynchronous events or Stream (Poll-based)
Layers can also have upto 5 versions and as they have versioning, they are also immutable.
Lambda Functions can use multiple layers which can be compatible with a runtime.
They are extracted to /opt directory as shown below:
Python - /opt/python/lib/python<version>/site-packages
Node.js - /opt/nodejs/node_modules
Binaries - /bin
Java - /opt/java/lib
Each layer has specific ARN.
Layers can be stacked and ordering of layer matters
Max size of a layer is 250 MB
Note that the id (which tell username) for an lambda environment generally tarts with sbx_user<number>
Example: 90% of invocations route to the stable version while 10% route to alias new-feature pointing to version 3. If the 10% is successful, deployment can continue until all traffic is migrated to version 3, and the stable alias is then pointed to version 3.
The event
parameter contains data that is passed to the Lambda function at the time of invocation. This data is specific to the event source that triggers the Lambda function.
Sample Code:
It is mostly a script (bootstrap executable file) that executes everything in order for getting the proper work from everyone and giving the output to user, via Runtime API and Lambda service
So, runtime runs the function's setup code. It reads the handler name from an environment variable, reads the invocation event from lambda runtime API, passes the event data to function handler and posts the response from handler back to Lambda. It can distributed as public or private layers
Working and Execution Flow can be explained as follows:
Invocation Request - GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next"
Invocation Response - POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/AwsRequestId/response"
Invocation Error - POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/AwsRequestId/error"
Invocation Error Path - POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error"
Example Scenario: Generate Symmetric keys using Lambda
Create a folder to host the source code
Edit the permissions for access to S3
For those interested, can head over API Gateway to have complete scenario based understanding.
Now, it should work
Lambda supports not only creating alias of a function but also supports routing to it. This is generally used to test new features by traffic shifting to send a small percentage of traffic to a second function alias or version for a rolling deployment before production rollout. This is commonly called a
When Lambda runs the function, it passes a context
object to the . This object provides methods and properties that provide information about the invocation, function, and execution environment. It's main role is to provide information about the current execution environment. Unlike event
, the methods and properties of the context
object remain the same regardless of the lambda was invoked or triggered
Runtime API Path ():
Now, to create a lambda function using dotnet cli, ensure that .NET SDK is installed from .