user2679290
user2679290

Reputation: 197

When does a new python session in AWS is created?

I have a scheduled event that triggers a lambda every two minutes in certain hours. Most of the time, as it does from file import lambda_handler, it maintains the same python session. Namely, the global variables are kept between runs.

Every several hours(not consistently), it reinitiate a python session. This results in a strange pattern, which I couldn't figure out the reasoning behind it. Is there?

Upvotes: 0

Views: 401

Answers (1)

sytech
sytech

Reputation: 41119

What you are observing is an artifact of 'cold starts' vs 'warm starts' in AWS Lambda. When functions are invoked within a short time period of each other, they may (or may not) reuse the same working container; this is a warm start.

Warm starts help optimize execution time. In this case, global objects are re-used for optimizing things like database connections or similar actions. Cold starts start a new container entirely, which means there's no existing state in memory. Cold starts also take somewhat longer, since it has to bootstrap the runtime again.

In the following code snippet, the CONNECTION object is global. The handler function checks to see if CONNECTION was already created and, if it is, it does not attempt to create a new one. If you were to invoke such a function many times in a short period and watch the log output for such a function, you would see it reuse the connection a lot.

logger.info('Cold Start')
CONNECTION = None
def my_handler(event, context):
    global CONNECTION
    if CONNECTION is None:
        logger.info('Initializing Connection')
        CONNECTION = make_connection()
    else:
        logger.info('reusing existing connection!')
    with CONNECTION:
        ... # do something

There is no set time period for which a warm start will occur, nor is there any guarantee that function invocations will be a warm start, even when they're called closely in time proximity. The Lambda service tries to decide whether to keep functions warm intelligently based on invocation patterns.

In some serverless frameworks like zappa there are configurations that invoke the function periodically (say, every 4 minutes) to attempt to keep it warm so that the next invocation of, say, a web request, will not bear the expense of a cold start.

Further reading:

https://medium.com/capital-one-tech/best-practices-for-aws-lambda-container-reuse-6ec45c74b67e

https://www.serverless.com/blog/keep-your-lambdas-warm

Upvotes: 2

Related Questions