LAmbda Flashcards
Lambda Execution Context
TMP Space
Large files can be written into a /tmp space, max is 512MB. These files can be used for multiple invocations.
Permeant persistence should be S3
How Throttling helps Lambda Concurrency ThrottlingReserved
QOS for lambda functions, limited bandwidth between different use bases. Multiple functions will use the same bandwith of concurrency limit, so if one function uses all 1000, like a ALB, then other items will be sluggish or be throttled.
Concurrency : Asnychronous
ExampleS3 Bucket upload triggering a Lambda Function, more than 1000 that occur will cause a error. Attempt to retry for 6 hoursretry interval goes from 1 second to 5 minutes
Lambda Tracing
1 _X_AMZN_TRACE_ID
2 AWS_XRAY_CONTENT_MISSING
3 AWS_XRAY_DAEMON_ADDRESS
environment variables to communicate with XRAYENABLE ACTIVE TRACING IN XRAY SECTION TO ALLOW
1 Containing tracing header
2 Log_error
3 XRAY daemon ip addressport
Policy and permissions:
happens automatically after enable active tracing.
in execution role
Xray:
PuttracesegmentsXray
PuttelemertrySegments
Lambda Access VPC, normally outside, can be done inside a VP
Lambda must be made in VPC
1. Define VPC ID, subnet and security group-Lamda will have its own ENI interface inside a subnet
- Lambda enters via ENI
- Need AWSLambdaExecutionRole
Lambda and Internet access
Default, no internet access Need Permissions to create ENI, Policy: AWSlambdaENIManagmentAccess:: ec2:Createnetworkinterface ec2:describenetworkinterface ec2:deletenetworkinterfaceDeploy in Public subnet,
will not give it internet access or a public IP. NEEDS TO BE DEPLOYED IN PRIVATE SUBNET WITH A NAT GATEWAY OR INSTANCE
Lambda must be placed in private subnet, can be routed online via a NAT gateway which needs to be located in separate public subnet
Public resources access from online, S3 or DynamoDB etc. These can be accessed via a VPC endpoint,
-S3 and Dynamo are Gateway endpoint-all others via a interface endpoint private link
Lambda Function Configuration:RAM
Ram must be increased to get more VCPU
1792 MB ram for each VCPU
More than one VCPU needs Multi-threading
MORE CPU HEAVY PROCESSES NEED MORE RAM
Cold start and Provision concurrency Init
Cold Start:
New instance, code is loaded outside handler run (init)
Initialization plus code will need to occur first at a cold start
Provisioned Concurrency helps reduce latency by creating execution environments ahead of invocations.
This combination helps to provide a consistent execution time even during special events that cause usage peaks. It also helps to optimize cost by limiting the time period.
Lambda Execution Context
Creates temporary runtime environment for any external dependencies of your code
Execution context includes /tmp directory.
Allows this runtime to be maintained to allow repeated invocations. saves time.
ALLOWS connections to be established outside handler and be used between function calls*
Top has the DB connection part of the function, to run the request each time, initiate many connections. the next allows one connection to DB to be used by many invocations.
——-def
connect to DBcall
db get itemsend
========
——–
connect to DB
def call db get items
Lambda Function Dependencies
DEPLOYMENT PACKAGE
Dependencies may be required to run code, this needs to be zipped into Lambda.
Less than 50MB goes to lambda
Else to S3
Node.js (Node_modules)
Python PIP (–target)
Java (.jar files)
Native libraries need to be compiled into AWS linux
SDK, comes by default.
in Lambda, Code entry type, Upload a Zip file
needs to be saved and wait until zip is uploaded and seen in directory tree
Lambda and Cloudformation
Works with cloudformation Inline: Simple code only Code.Zipfile property is used Cannot use function dependancies
or Zip file and S3
Zip stored in S3
S3 Zip location must be reference in code via following parameters in cloud formation
Cloud formation parameters
S3Bucket
S3Key: Full path to Zip
S3ObjectVersion, if bucket is versioned
If you update code in S3, but do not update above, then cloud formation wont update your function.
Lambda Layers
Custom Runtimes
C++
Rust
Externalize Dependencies
Package heavy portions of function library files into layers, that can be referenced by separate functions.
Function 1 and function 2 will both reference layer that contains heavy library file 1.**
*Layer is underneath function lambda code middle section. you will select a layer or provide one.
**ALLOWS IMPORTING CODE THAT YOU CANNOT RUN NATIVELY, USE A LAYER FOR THIS ALSO!
Lambda Versions And Aliases
$Latest (mutable)
V1 : Immutable
Each version get their own ARN, the version is a code and configuration. Each version and latest should be able to be accessed.
Aliases, they point to different lambda versions
EXAMPLE: Can define a Dev/Test/prod aliases and have them point to different lambda versions. These aliases are mutable!
Use case:
Blue Green deployment, allows traffic to be directed based on weight to make sure function works before full traffic switches
Own ARN
Cannot reference other aliases
LAB:
Qualifiers: show information about version and aliases
Actions: We can publish versions
Next to both we then can switch to the version we like, which is immutable.
Aliases, action can allow creation of aliases which will point to specific versions.
Blue Green deployment, Under Alias configuration, you can another version and assign a weighting on the alternative version.
Cold start and Provision concurrencyInit
Cold Start:
New instance, code is loaded outside handler run (init)
Initialization plus code will need to occur first at a cold start
Provisioned Concurrency helps reduce latency by creating execution environments ahead of invocations.
This combination helps to provide a consistent execution time even during special events that cause usage peaks. It also helps to optimize cost by limiting the time period.
Lambda Reserved and Provisioned Capacity
How to manage bandwidth of 1000 invocations at a time.
When a function has reserved concurrency, no other function can use that concurrency. … By allocating provisioned concurrency before an increase in invocations, you can ensure that all requests are served by initialized instances with very low latency.
Lambda Function Dependances
Dependencies may be required to run code, this needs to be zipped into Lambda.
Less than 50MB goes to lambda
Else to S3
Node_modules Node.js
Python PIP –target
Java .jar files
Native libraries need to be compiled into AWS linux
SDK, comes by default.
Lambda and Cloudformation
Works with cloudformation Inline: Simple code only Code.Zipfile property is used Cannot use function dependancies
or Zip file and S3
Zip stored in S3
S3 Zip location must be reference in code via following terms
S3Bucket
S3Key: Full path to Zip
S3ObjectVersion, if bucket is versioned
If you update code in S3, but do not update above, then cloud formation wont update your function.
Lambda Layers
Custom Runtimes
C++
Rust
Externalize Dependencies
Package heavy portions of function library files into layers, that can be referenced by separate functions.
Function 1 and function 2 will both reference layer that contains heavy library file 1.**
*Layer is underneath function lambda code middle section. you will select a layer or provide one.