AWS Lambda caching in Python

· Thomas Taylor

Caching between lambda invocations may be useful for certain scenarios:

How to cache between lambda invocations

In Python, it’s as simple as modifying a global variable.

1obj = None
3def lambda_handler(event):
4    global obj
5    if not obj:
6        obj = YourClass(event)

Upon initial run of a lambda function, obj will be set to the output of YourClass(event). On subsequent warm starts, the lambda function will reuse the global obj from the previous lambda run. More information regarding warm vs cold starts can be found here.


Reply to this post by email ↪