Skip to content

Feature request: Invocation context manager #2068

Open
@ran-isenberg

Description

@ran-isenberg
Contributor

Use case

  1. I want to store global variables so they can be fetched in multiple areas in my areas without having to explicitly passing them around into the inner functions.
  2. I want these variables to get cleaned automatically on every invocation
  3. I want to have the option to automatically add these variables the logger so they are logged. But i want to also have the option not to do that. for example: log tenant id, but dont log the user's email address.
  4. I want to have a simple get function to get these parameters by name.
  5. I'd like to have some default function getters such as tenant id, username.

Solution/User Experience

A singleton class that has several functions:

  1. store value - store(key: str, value: Any, should_log : bool)
  2. get value - get(key)
  3. get_tenant_id
  4. get_username
  5. store_tenant_id(tenant_id : str)
  6. store_username(username: str)

Alternative solutions

No response

Acknowledgment

  • This feature request meets Lambda Powertools Tenets
    Should this be considered in other Lambda Powertools languages? i.e. Java, TypeScript

Activity

added
need-customer-feedbackRequires more customers feedback before making or revisiting a decision
and removed
triagePending triage from maintainers
on Apr 4, 2023
heitorlessa

heitorlessa commented on Apr 4, 2023

@heitorlessa
Contributor

Thanks a lot for taking the time to create this feature request @ran-isenberg! Adding labels to hear from other customers, and adding to the bucket of ideas for now.

I understand the intent, and we're also seeing a resurgence of customers using threading and asyncio in Lambda - this can get trickier and difficult to set expectations if we're not careful (e.g., ContextVars).

Looking forward to hearing from more customers on this need, and solutions they're using today (e.g., global dict, global dataclass, etc.)

Thanks!!

ran-isenberg

ran-isenberg commented on Apr 4, 2023

@ran-isenberg
ContributorAuthor

We have a global class (with a dict). The best practice as I see it, is that the handler inject the parameters straight after the input validation. Then, if you tigger multiple threads, they are all using the same instance with read only capabilities.

sthulb

sthulb commented on Apr 5, 2023

@sthulb
Contributor

I have a few questions :)

How do you see this working?

Where would the storage be for the data it would pull in? or would you expect there to be multiple data providers in case someone wants to use Redis/DDB/SQL/?

What would the failure modes be? i.e. not being able to refresh the context, would you want to error the invoke?

ran-isenberg

ran-isenberg commented on Apr 5, 2023

@ran-isenberg
ContributorAuthor

It's not intended for anything else other than input based session context. No DDB/Redis.. just plain in memory that is reset once the invocation is finished.

Handler gets input -> verifies input -> you extract tenant id, username etc. from the input/ auth header id token etc. , set it on the context and call the logic layer.
again, super simple, just intended to remove the need to pass these parameters into the logic layer and have globally accessible.

ran-isenberg

ran-isenberg commented on Apr 5, 2023

@ran-isenberg
ContributorAuthor

I have a few questions :)

How do you see this working?

Where would the storage be for the data it would pull in? or would you expect there to be multiple data providers in case someone wants to use Redis/DDB/SQL/?

What would the failure modes be? i.e. not being able to refresh the context, would you want to error the invoke?

I didnt intend it as a multi-invocation session context, maybe that's the confusion.

changed the title [-]Feature request: Session context manager[/-] [+]Feature request: Invocation context manager[/+] on Apr 5, 2023
ran-isenberg

ran-isenberg commented on Apr 5, 2023

@ran-isenberg
ContributorAuthor

renamed to invocation context manager to make it more accurate. @heitorlessa

heitorlessa

heitorlessa commented on Jun 23, 2023

@heitorlessa
Contributor

cc @dreamorosi @am29d as this was a similar ask in TS.

added
on-holdThis item is on-hold and will be revisited in the future
on Feb 19, 2025
dreamorosi

dreamorosi commented on Feb 19, 2025

@dreamorosi
Contributor

Just wanted to chime in here to share this year's roadmap for the Python version of Powertools for AWS Lambda.

At least for now, this type of feature is out of scope, not in small part for the lack of wider customer demand.

The feature request will stay open, if you're interested in us considering this, please add a 👍 to the original post and consider sharing your use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestfeature requestneed-customer-feedbackRequires more customers feedback before making or revisiting a decisionon-holdThis item is on-hold and will be revisited in the future

    Type

    No type

    Projects

    Status

    Ideas

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sthulb@heitorlessa@dreamorosi@ran-isenberg

        Issue actions

          Feature request: Invocation context manager · Issue #2068 · aws-powertools/powertools-lambda-python