Comments (5)
This explains it, thank you @pkit!
from mangum.
Hey @pkucmus
I'm new to Mangum but I'm going to try to answer your question from my point of view,
AWS Lambdas are ephemeral, and will exist only when the application is invoked, this have 2 results:
- the storage is temporary, cannot be shared between lambdas, so you ll have to use some other services like DynamoDB.
- probably you don't need a connection pool for Postgres here, but in case you need a connection pool, you can create it manually in the application's script, but make sure to use the smallest possible connection pool, otherwise you ll reach the limit of pg-open-connection.
In case your JWKS doesn't change too much, you can set them as env-vars to the AWS Lambda function, or save them in Parameter Store.
Hope this answer helps you, as I said, this is from my point of view, probably someone else has a better idea about this case.
from mangum.
@pkucmus All the init should be done in in your main.py
(or whatever you call it)
On "warm" restarts, the once initialized data in main will remain there (good case for a cache, a connection pool or any other ephemeral state)
On "cold" restarts the main.py
will be run again and all the state will be reinitialized.
In case you want to save a "permanent" state, the best option is Dynamo.
As it's fast and persistent. And doesn't cost exorbitant amounts of money, like AWS hosted Redis.
Secrets can be stored in SSM parameter store, and it's also a pretty fast fetch.
from mangum.
@pkit thanks, so in conclusion if I have code like this:
something_to_keep = fetch_the_something()
def handler(event, context):
use_the_something(something_to_keep)
if handler
is my lamdba handler only the code in handler
will be invoked on a warm start? Once something done outside of the main handler it will remain in it's state between cold starts, right?
Sounds like global
could be useful here in some cases? Like:
something_to_keep = None
def handler(event, context):
global something_to_keep
if something_to_keep is None:
something_to_keep = fetch_the_something()
use_the_something(something_to_keep)
not that the example is good, but the above should be possible and should keep the state of something_to_keep
between cold starts?
from mangum.
if handler is my lamdba handler only the code in handler will be invoked on a warm start? Once something done outside of the main handler it will remain in it's state between cold starts, right?
Yes and yes
Sounds like global could be useful here in some cases?
Yup you can use global
you can also use imports.
Like:
some.py
something = fetch_the_something()
main.py
(or anywhere else)
from some import something
Imports in python are imported only once. So it's guaranteed to run once even if you import it from multiple places.
from mangum.
Related Issues (20)
- Support for S3 Events HOT 3
- AWS lambda function not identify the handler in FastAPI project
- FYI: documentation is not available at mangum.io HOT 4
- How To: How to form SQS event to look like REST body as if coming from API Gateway HOT 1
- Support for streaming? HOT 3
- Problems with Sanic 23.3.0 and Mangum 0.17.0 HOT 5
- Wrong domain when using AWS Lambda FunctionUrls and CloudFront with FastAPI
- Handle comma separated X-Forwarded-port
- SQS message not returned back to the topic HOT 1
- FastAPI redirects to wrong host with Mangum HOT 2
- Doesn't work with Lambda in Docker locally HOT 2
- Mangum breaks Django POST with multipart/form-data; HOT 2
- `OSError` in Vercel Serverless Functions deployment
- Official support for Python 3.11 and Python 3.12? HOT 1
- `Request.url` does not include the api_gateway_base_path
- VPC Lattice event support
- Dealing with long running client HOT 4
- mangum.io Domain Expired HOT 2
- Support lifespan state
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mangum.