Code Monkey home page Code Monkey logo

Comments (5)

pkucmus avatar pkucmus commented on June 16, 2024 1

This explains it, thank you @pkit!

from mangum.

tahayk avatar tahayk commented on June 16, 2024

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:

  1. the storage is temporary, cannot be shared between lambdas, so you ll have to use some other services like DynamoDB.
  2. 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.

pkit avatar pkit commented on June 16, 2024

@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.

pkucmus avatar pkucmus commented on June 16, 2024

@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.

pkit avatar pkit commented on June 16, 2024

@pkucmus

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.