Skip to content

Commit

Permalink
Drastically reduce cold start times by calling LambdaHandler external…
Browse files Browse the repository at this point in the history
…ly (#982)

* ADD: LambdaHandler outside
* UPDATE: apply to INSTANTIATE_LAMBDA_HANDLER_ON_IMPORT variable

Co-authored-by: Will Boyce <[email protected]>
  • Loading branch information
xncbf and wrboyce authored Dec 9, 2021
1 parent d359e22 commit f5efdcd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
- [Application Load Balancer Event Source](#application-load-balancer-event-source)
- [Endpoint Configuration](#endpoint-configuration)
- [Example Private API Gateway configuration](#example-private-api-gateway-configuration)
- [Cold Starts (Experimental)](#cold-starts-experimental)
- [Zappa Guides](#zappa-guides)
- [Zappa in the Press](#zappa-in-the-press)
- [Sites Using Zappa](#sites-using-zappa)
Expand Down Expand Up @@ -1443,6 +1444,10 @@ apigateway_resource_policy.json:
}
```

### Cold Starts (Experimental)

Lambda may provide additional resources than provisioned during cold start initialization. Set `INSTANTIATE_LAMBDA_HANDLER_ON_IMPORT=True` to instantiate the lambda handler on import. This is an experimental feature - if startup time is critical, look into using Provisioned Concurrency.

## Zappa Guides

* [Django-Zappa tutorial (screencast)](https://www.youtube.com/watch?v=plUrbPN0xc8&feature=youtu.be).
Expand Down
7 changes: 6 additions & 1 deletion zappa/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ def import_module_and_get_function(whole_function):

@classmethod
def lambda_handler(cls, event, context): # pragma: no cover
handler = cls()
if not os.environ.get("INSTANTIATE_LAMBDA_HANDLER_ON_IMPORT"):
handler = cls()
exception_handler = handler.settings.EXCEPTION_HANDLER
try:
return handler.handler(event, context)
Expand Down Expand Up @@ -663,3 +664,7 @@ def keep_warm_callback(event, context):
event={}, context=context
) # overriding event with an empty one so that web app initialization will
# be triggered.


if os.environ.get("INSTANTIATE_LAMBDA_HANDLER_ON_IMPORT"):
handler = LambdaHandler()

0 comments on commit f5efdcd

Please sign in to comment.