- 最新
- 投票最多
- 评论最多
Hi Lookatthisgraph,
It looks like you are experimenting with Lambda runtime environments!
== A Short Answer And Why It Is Not A Bug But A Feature ==
Well, according to the mighty internet:
Possibly the simplest design pattern is the singleton, which is a way to provide one and only one object of a particular type...
https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Singleton.html
And based on the fact you have created it outside of the handler, and also according to the following article (it's using node as an example but this behaviour is same across different runtimes):
The first time a function executes after being created or having its code or resource configuration updated, a new container with the appropriate resources will be created to execute it, and the code for the function will be loaded into the container. In nodejs, initialization code is executed once per container creation, before the handler is called for the first time.
https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/
So this is the normal behaviour from the Lambda container, and in fact, many of our customer is utilizing this feature actively.
== How Can I Use This Feature ==
So, the takeaway from the best-practices:
Take advantage of execution context reuse to improve the performance of your function. Initialize SDK clients and database connections outside of the function handler, and cache static assets locally in the /tmp directory. Subsequent invocations processed by the same instance of your function can reuse these resources. This saves execution time and cost.
To avoid potential data leaks across invocations, don’t use the execution context to store user data, events, or other information with security implications. If your function relies on a mutable state that can’t be stored in memory within the handler, consider creating a separate function or separate versions of a function for each user.
https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html
Hope this answers your question.
相关内容
- AWS 官方已更新 1 年前
- AWS 官方已更新 4 个月前
- AWS 官方已更新 1 年前