I’m currently working on a Python 3.11 Lambda function for a REST API using AWS Powertools, and I’m struggling with its cost start/initialisation duration, which is currently between 3-5 seconds.
Here’s what I’ve done so far:
Upgraded to Python 3.11 and switched to arm64 architecture
Layer Size: I’ve managed to reduce the layer size down to 14.1 MB by including only minimal dependencies (AWS Powertools, Stripe, CognitoJWT).
Lambda Asset Size: The Lambda asset is now at 292 KB.
Code Optimization: I’ve optimized the Python code by precompiling it using PYTHONNODEBUGRANGES=1 python3.11 -m compileall -o 2 -b ..
My codebase currently has about 5.8k lines of code, and it covers every route for the REST API. I’m unsure if there are any additional optimisations I can make without splitting the Lambda function. Would dynamically importing modules based on the route improve initialisation time?
How would I be able to use other dependencies with the AWS powertools managed layer? At the moment I’m downloading the managed layer and adding stripe, congitojwt etc.
Lambda Is very efficient at loading code and you are not loading much code.
You are likely doing something in the INIT phase that takes quite a bit of time. Or several things all taking a bit of time, sequentially.
For instance if you are establishing different HTTP connection against Stripe and Cognito, that would already represent something.
Fastest way to debug this, not best - fastest, is to just print the time.
Print the time at the very top, between imports, at the end of the imports, when the function is reaching the main. And along all the init code that you suspect is the culprit.
Consider what work you can do in parallel with multiple threads.
There are a few blog posts on this, but it depends on the deployment. Since lambda containers are based on a highly optimized base image, it’s mostly cached. Above a certain code base size, containers are often faster. For small functions, they’re usually slower. Container start times are getting faster and faster though, and the DX alone makes them my preferred choice even for small stuff.
Python Lambda should start anywhere from 80-200ms. Generally, splitting this much code into multiple Lambda’s and orchestrating with Step Functions and API Gateway should be better.
Not in all use cases, I have Python 3.11 lambdas that use Docker, I went with that approach because my use case needs some binaries that weight too much, init times are not that high:
```
Duration: 4667.64 ms Billed Duration: 5882 ms Memory Size: 1024 MB Max Memory Used: 190 MB Init Duration: 1213.79 ms
```
PD: Memory size is set to that value to get more vCPU power since my workload is mainly CPU-bounded.
Ok thanks! I’ve managed to get cold-start down to around 1.4s using separate layers along with other optimisations. Still quite high but quite a significant drop.
I’ve enabled X-ray traces but I can’t seem to view any more details about the ‘Initialization’ segment other than the length of time. Is there anywhere I need to look specifically?