Lambda - MacKittipat/note-developer GitHub Wiki
Concepts
- Compute service that lets you run code without provisioning or managing servers.
- 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.
- Event
- An event is a JSON-formatted document that contains data for a Lambda function to process
- Execution environment
- An execution environment provides a secure and isolated runtime environment for your Lambda function
- When you create your Lambda function, you specify configuration information, such as the amount of memory available and the maximum execution time allowed for your function. Lambda uses this information to set up the execution environment.
- Deployment package
- A .zip file archive that contains your function code and its dependencies
- A container image that is compatible with the Open Container Initiative (OCI) specification. You add your function code and dependencies to the image. You must also include the operating system and a Lambda runtime.
- Layer
- A .zip file archive that can contain additional code or other content. A layer can contain libraries, a custom runtime, data, or configuration files.
- 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.
Programming Model
- Node.js, Python, Java, Go, Ruby, C#, PowerShell
Invoking Function
- Synchronous invocation
- Lambda runs the function and waits for a response. When the function completes, Lambda returns the response from the function's code with additional data, such as the version of the function that was invoked
- If the Lambda function fails, retries are the responsibility of the trigger.
- Example AWS service that invoke Lambda Synchronously : Amazon API Gateway, Amazon Cognito
- Asynchronous invocation
- Lambda places the event in a queue and returns a success response without additional information. A separate process reads events from the queue and sends them to your function.
- If the function returns an error, Lambda attempts to run it two more times
- Example AWS service that invoke Lambda Asynchronously : S3, SNS
- Polling invocation or Event source mappings
- An event source mapping is a Lambda resource that reads from an event source and invokes a Lambda function. You can use event source mappings to process items from a stream or queue in services that don't invoke Lambda functions directly
- Event source mappings read items from a target event source. By default, an event source mapping batches records together into a single payload that Lambda sends to your function
- Example AWS service : Amazon DynamoDB Streams, SQS, Amazon Kinesis
- https://docs.aws.amazon.com/lambda/latest/operatorguide/invocation-modes.html
- https://aws.amazon.com/blogs/architecture/understanding-the-different-ways-to-invoke-lambda-functions/
Scaling and Throughput
-
Concurrency of a system is the ability to process more than one task simultaneously
- Number of in-flight requests your AWS Lambda function is handling at the same time
- For each concurrent request, Lambda provisions a separate instance of your execution environment
- As your functions receive more requests, Lambda automatically handles scaling the number of execution environments until you reach your account's concurrency limit.
- By default, Lambda provides your account with a total concurrency limit of 1,000 across all functions in a region
- The burst concurrency quota provides an initial burst of traffic, between 500 and 3000 per minute, depending on the Region
-
Throughput is transactions/requests
- If a function takes 1 second to run, and 10 invocations happen concurrently, Lambda creates 10 execution environments. In this case, Lambda processes 10 requests per second.
- If the function duration is halved to 500 ms, the Lambda concurrency remains 10. However, transactions per second are now 20.
-
https://docs.aws.amazon.com/lambda/latest/dg/lambda-concurrency.html
Reserved concurrency and provisioned concurrency
- Reserved concurrency to reserve a portion of your account's concurrency for a function. This is useful if you don't want other functions taking up all the available unreserved concurrency.
- Provisioned concurrency to pre-initialize a number of environment instances for a function. This is useful for reducing cold start latencies.
- Provisioned concurrency comes at a cost. You are billed for provisioned concurrency
Cold start
- A Lambda cold start occurs when a new function instance must be created and initialized. The cold start refers to the delay between invocation and runtime created by the initialization process.
- Cold start occur when
- Code is updated.
- Instances have expired due to inactivity
- Cold start duration is #1 and #2
- Download our code
- Start new execution environment
- Execute initialization code
- Execute handler code
- Reduce cold start
- Provisioned concurrency
- Request a given number of workers to be always-warm and dedicated to a specific Lambda
- Lambda SnapStart (For Java)
- Keep Lambda function warm by setup ping scheduler
- The idea is to have a CloudWatch timer that fires every few minutes and sends N parallel requests to the target Lambda. If all those requests land at the same time, AWS has to provision at least N workers to process them
- Choose the right language
- Provisioned concurrency