330 words
2 minutes
AWS Lambda
Start up
A normal AWS lambda startup has the two part Init
and Invoke
Init
:
- Init AWS lambda runtime
- Download code or Download docker image (If use docker image as runtime layer)
- Start execution environment (Start code or start docker image)
- Execute init code
Invoke
: 5. Execute invoke code (code in handler)
Init
procedure only runs one time for one single AWS lambda instance.
Use python code as example
import tidy3d
print("init process")
a=1+1
def handler(event, context):
print("hello world")
AWS lambda use handler(event,context)
as entry point.
The Init code procedure
includes import tidy3d
, print("init process")
and a=1+1
The Invoke code proecure
includes all codes written in handler
.
How to reduce the start up cost
Concurrent Execution
Partial Failure
Comes a batch whose size is 50, it throws error when process the 30th records. We want to tell aws lambda we only need to retry the unprocessed 20 records. You can use the following code to do the retry.
exports.handler = async function (event){
try{
}catch (e){
return {batchItemFailures: [{itemIdentifier: `the error record's SequenceNumber`}]}
}
return {}
}
Note: remember use async
function or use input attribute callback
to return, otherwise the return will not work.
Events:
Flow360SolverUpdate:
Type: DynamoDB
Properties:
Stream: !Ref StreamArn
StartingPosition: TRIM_HORIZON
BatchSize: 50
ParallelizationFactor: 1 # The number of batches to process concurrently from each shard. If you do not care about the order of the message, you can set it up to 10
Enabled: true
MaximumBatchingWindowInSeconds: 0
MaximumRecordAgeInSeconds: -1 # never discards old records.
BisectBatchOnFunctionError: true # split the failed batch into two batches and retry, will reset the retry count if batch size > 1
FunctionResponseTypes:
# means same batch will retry 3 times, if all 3 times failed, the batch will be sent to dead letter queue.
# If BisectBatchOnFunctionError is set to true, the batch will be split into two batches and retry. The retry time will reset # Only when batch size is declined to 1, after three times retry, the message will be sent to dead letter queue - ReportBatchItemFailures MaximumRetryAttempts: 3
DestinationConfig:
OnFailure:
Destination: !Sub arn:${AWS::Partition}:sqs:${AWS::Region}:${AWS::AccountId}:${DeadLetterQueueName}