Are AWS Lambda libraries loaded every time?

I am looking at AWS Lambda to create a Python function that will process the data. I need to load a heavy model to run my script (word2vec trainable model), it takes about 5 minutes to do it on my computer for example. But once it is loaded, the function is executed very quickly. If I am using AWS Lambda, will this model be loaded only once, or will it be loaded every time I call my function?

Thank,

+3


source to share


2 answers


May be.

AWS Lambda uses reusable containers. So, for your use case, the Lambda function will execute quickly if it happened in an already initialized container. Otherwise it will be slow. However, you cannot predict the behavior.

Relevant documentation:



The first time a function is run after it has been created, or the code or resource configuration is 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.

Let's say your function ends and it passes for a while, then you call it again. Lambda can create a new container again, in which case the experience is as described above. This will be true if you change your code. However, if you haven't changed the code and it hasn't been too long, Lambda can reuse the previous container.

Remember, you cannot depend on reusing a container, as its prerogative Lambdas creates a new one instead.

  1. More official documentation here .
0


source


This will MAYBE (thanks to Michael-sqlbot for the correction) load every time you call Lambda.

We can infer that AWS Lambdas are stateless based on the following

  • Lambda is not stateless

    Lambda functions are "stateless" without being tied to the underlying infrastructure, so Lambda can quickly run as many copies of the function as needed to scale to the speed of incoming events.

  • Lambda must be encoded in stateless style

    Your Lambda function code should be written in a stateless style and have no affinity for the underlying compute infrastructure. Your code should expect local file system access, child processes, and similar artifacts to be limited by the lifetime of the request.



However, Reusing a container is possible in Lambda

If you haven't changed the code and it hasn't been too long, Lambda can reuse the previous container.

So, to answer your question, perhaps you will return a model, and the likelihood of doing so is inversely proportional to the time interval between two Lambda calls. But you just can't rely on it

0


source







All Articles