Skip to content

Enhancement request: option to run LifespanCycle on Mangum __init__ rather than on each __call__ #342

@jrobbins-LiveData

Description

@jrobbins-LiveData

While in some sense "Lambda is stateless", in practice AWS recommends

Initialize SDK clients and database connections outside of the function handler, and cache static assets locally in the /tmp directory. Subsequent invocations processed by the same instance of your function can reuse these resources. This saves cost by reducing function run time.

If I instance Mangum outside of my function handler, it would be great to have the option for my lifespan asynccontextmanager to run once, e.g. at the onset of the first handler call, rather than on every call. This would support use-cases like caching a database connection per AWS recommendation without resorting to workarounds.

This option would allow treating the model of lifespan to cover the actual lifespan of the Lambda function instance, and so, at least to me, seems like a "natural" mapping of the concept of "lifespan".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions