Replies: 1 comment 2 replies
-
I suggest reading through this article on AWS docs: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html In the background, the Lambda service manages execution environments for your function. When your function is invoked, Lambda checks if there is an execution environment ready to use. If there isn't, one is initialized (this is what is usually refered to as a cold start). Further invocations are then handled by this intialized function. If no invocations happens during a period of time, Lambda shuts down the execution environment. If a new invocation occurs, Lambda then has to initialize a new environment again. This environment will not retain the global state of previous execution environments. So, to answer your last question. If you use global state, it will stay within the same execution environment. But if a new environment is started it will be reset. So I would avoid using global state. This can however be used to cache heavy stuff like external clients (such as boto3 clients) within the environment, to avoid recreating a new client on each request. |
Beta Was this translation helpful? Give feedback.
-
My question is more about Lambda and less about Mangum itself. I have been lately struggling with this.
So Lambda invokes a chunk of code every time it is invoked. And Mangum makes it possible to route multiple paths to same Lambda instead of per lambda per route.
My question is.. Is the FastAPI server always running in the background between the invocation of Lambda? Or it is started every time is invoked?
I am asking this because, what if I have a global state which I use over lifetime of my application? Will it be reset between the invocation of the Lambda?
Beta Was this translation helpful? Give feedback.
All reactions