Clever Castle
330 words
2 minutes
AWS Lambda

Start up#

A normal AWS lambda startup has the two part Init and Invoke

Init :

  1. Init AWS lambda runtime
  2. Download code or Download docker image (If use docker image as runtime layer)
  3. Start execution environment (Start code or start docker image)
  4. 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}
AWS Lambda
https://blog.ivyxjc.com/posts/aws-lambda-01/
Author
ivyxjc
Published at
2023-09-07