This is the note for AWS Lambda. The content was from Udemy course AWS Lambda - A Practical Guide from Daniel who has a Youtube channel Be A Better Dev
Presentation from Daniel can be found below: https://docs.google.com/presentation/d/1UBgzd0WdBlKygAuTEEhFTMrRiwTKD5ebZvcaYzL68BE/edit#slide=id.g35f391192_04
- Start with low memory settings
- Once collecting enough invocation history, use the Compute Optimizer Tool to optimze memory provision and running time of function
- 1 Million free requests per month and 400,000 GB Seconds
- Use lambda cost calculator
- Steps:
- Code download:
- Depending on programming language and whether we run that lambda or not
- Start execution environment
- Execute init code: It is everything outside Lambda's handler function
- Try to specific when import environment
- Execute handler code
- Code download:
- Strategies to minimize cold start
- Minimize number of library dependencies
- Only import what you need
- Raise memory configuration: It is not only increasing memory but also computer nodes behind the scene: It can be an expensive option
- Utilize provisioned concurrency: Keep Lambda always in the warm state by having some lambda containers behind the scene. It is an expensive option
- Number of requests being served at a given moment
- New containers are spawned for each concurrent request
- Concurrency is a major scaling consideration, and can cause applications to fail due to Throttling
- Default 1000 units of concurrency per AWS account per region.
- Unreserved, Reserved, and Provisioned
- Throttling is when Lambda rejects a request
- Occurs when in flight invocations exceeds available concurrency
- Alarm on throttles for early indicators of issues by Cloudwatch
- Evaluate your concurrency needs and plan accordingly
- Have your clients use Exponential Backoff to avoid retry storms
- Raising Memory Limit can help, but be careful
- Use provision concurrency to avoid cold starts, but be careful
- Version is optional.
- New upload default to $LATEST version. When create a version, $LASTEST is assigned automatically to the new version if the new version does not have any specific name.
Example of Aliases:
- In the begining, we creat Python version 1, and assign Alias prod to that version:
- Now we have version 2, $LATEST version is automatically moved from version 1 to version 2. Alias is still kept at version 1.
-Then we create version 3, again $LATEST version is automatically moved from version 2 to version 3. Version 3's performance is good, thus we decide to use it. So we move prod Alias to version 1 to version 3 to direct all flow to that version.
- Interestingly, we can assign weight to different version/alias
- AWS environment is stored. We can access it with os.environ
- We can also add new key, value into the environment
- The below code in Lambda would list all AWS environment's keys and values to dictionary.
import os
def lambda_handler(event, context):
allAWSEnv = os.environ
print(f"This is all environment variables: {allAWSEnv}")
# I added a environment variable son_pham
# Now I access and print that variable out
sonPham = os.environ["son_pham"]
print(f"This is the value of environment variable son_pham: {sonPham}")
- Is isolated and private network within AWS
- Public abd Private Subnets
- Critical for security and apps with compliance guidelines
- Only necessary when the function needs to access resources not accessible over public internet such as RDS or ElasticSearch in private subnet
- Behind the scenes, Lambda creates Elastic Network Interfaces (ENI)s for each subnet the function is deployed into. This used to be the issue with cold start latency, but not anymore.
- VPC Endpoints can be used to communicate with some AWS services privately
- Below is an example that Lambda cannot access directly to internet, but have to go through NAT Gateway in Public Subnet, and NAT Gateway to got Internet Gateway to go out. To connect S3, instead of going out to internet to connect (which cause latency and security issue), we can config VPC for Lambda to connect with S3.
- All logs are automatically saved in CloudWatch
- Allow to write patterns (similar to REGEX) to extract metrics from log times
- Much cheaper than PutMetric
- More suitable for high traffic application
- Search for patterns across multiple functions at one
- Pay per use
- Query similar to SQL
- Don't be overly verbose with Logging
- Embed log lines with important ids to make tracing easier
- Use Cloudwatch Logs Insights for fuzzy searching
- Set up a log retention policy or archive regularly
Create the below function to the result when having error
def lambda_handler(event, context):
print("UploadError 1")
Then we go to CloudWatch to setup the metric filters in Log groups after selecting the targetted function.
We then define the pattern to capture, and test if the pattern is working
Move to the next page to complete the setup for metric filter.
We can use an open-source step function AWS Lambda Power Tuning to optimize memory and cost
https://github.com/alexcasalboni/aws-lambda-power-tuning
We use the above tool to optimize the below function
def lambda_handler(event, context):
fibonacci_of(100)
def fibonacci_of(n):
if n in {0,1}: # Base case
return n
return fibonacci_of(n-1) + fibonacci_of(n-2) # Recursive case
The below input is used to run the tuner:
{
"lambdaARN": "Lambda function arn",
"powerValues": [
128,
256,
512,
1024
],
"num": 10,
"payload": "{}",
"parallelInvocation": true,
"strategy": "cost"
}