Skip to content

Feature request: Idempotency prefix is susceptible to refactoring #5897

Description

@lbustelo

Use case

Problem:

Using aws-lambda-powertools==3.4.1

The prefix to the Idempotency key is hardcoded to force a fully qualified name based on the location of the function that uses the decorator. There is no simple way of modifying this behavior in order to make that prefix some static value.

persistence_store.configure(config, f"{self.function.__module__}.{self.function.__qualname__}")

This makes the Idempotency susceptible to future code refactoring. To prevent duplicate handling you can either

  1. Never move or rename the function decorated with @idempotent_function
  2. Or, migrate all the keys in your Persistent Store

Solution/User Experience

A solution is to provide a way to override the key prefix on the @idempotent_function function. An optional argument like idempotency_key_prefix that allows a user to provided a fix string. This would allow freedom of refactoring (or even have multiple functions that are controlled by the same idempotency key space).

Considerations:

  • This should only control the prefix. The standard behavior of appending #<hash of arguments> would stay intact
  • Not sure if this new feature should force the user to specify the entire prefix, or only the part that goes after the <lambda_name>. I lean towards the full prefix, and let the user decide how to fully qualify the key.

Alternative solutions

Acknowledgment

Activity

  1. added
    feature-request
    triage
    on Jan 22, 2025
  2. boring-cyborg commented on Jan 22, 2025

    @boring-cyborg

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

  3. dreamorosi commented on Jan 22, 2025

    @dreamorosi
    Contributor

    Hi @lbustelo, thank you for opening the issue.

    I'm not against adding this feature as long as we add a clear disclaimer to the docs / API docs explaining that changing this value on existing functions will void the idempotency.

    Regarding the implementation, I'd agree with your suggestion of replacing the full prefix. Basically you'd either let us decide the prefix, or take full control of it.

    Will wait for other maintainers to weigh in before adding it to the backlog.

  4. removed
    triage
    on Jan 22, 2025
  5. moved this from Ideas to Pending review in Powertools for AWS Lambda (Python)on Jan 22, 2025
  6. am29d commented on Jan 22, 2025

    @am29d
    Contributor

    Hey @lbustelo , thanks for opening a feature request.

    This would certainly help during refactoring and also give more freedom to pick your own key. I agree with @dreamorosi to leave it to user completely once they want to customize it. That will create clear responsibility boundary between library authors and users.

  7. moved this from Pending review to Working on it in Powertools for AWS Lambda (Python)on Jan 22, 2025
  8. moved this from Working on it to Ideas in Powertools for AWS Lambda (Python)on Jan 22, 2025
  9. leandrodamascena commented on Jan 22, 2025

    @leandrodamascena
    Contributor

    Hi @lbustelo! Thanks for opening this issue. I'm not very happy with the name of the idempotency_key_custom_prefix parameter and I think we can find a better one.

    Not sure if this new feature should force the user to specify the entire prefix, or only the part that goes after the <lambda_name>. I lean towards the full prefix, and let the user decide how to fully qualify the key.

    I agree with letting the customer decide what they want as a prefix and not making assumptions here.

    PR: #5898

    The experience might be like:

    Decorating standalone functions

    import os
    from dataclasses import dataclass
    
    from aws_lambda_powertools.utilities.idempotency import (
        DynamoDBPersistenceLayer,
        IdempotencyConfig,
        idempotent_function,
    )
    from aws_lambda_powertools.utilities.typing import LambdaContext
    
    table = os.getenv("IDEMPOTENCY_TABLE", "")
    dynamodb = DynamoDBPersistenceLayer(table_name=table)
    config = IdempotencyConfig(event_key_jmespath="order_id")  # see Choosing a payload subset section
    
    
    @dataclass
    class OrderItem:
        sku: str
        description: str
    
    
    @dataclass
    class Order:
        item: OrderItem
        order_id: int
    
    
    @idempotent_function(
        data_keyword_argument="order",
        config=config,
        persistence_store=dynamodb,
        idempotency_key_custom_prefix="my_custom_prefix",
    )
    def process_order(order: Order): 
        return f"processed order {order.order_id}"
    
    
    def lambda_handler(event: dict, context: LambdaContext):
        config.register_lambda_context(context)
        order_item = OrderItem(sku="fake", description="sample")
        order = Order(item=order_item, order_id=1)
        process_order(order=order)

    Decorating the Lambda handler

    from aws_lambda_powertools.utilities.idempotency import (
        DynamoDBPersistenceLayer,
        idempotent,
    )
    from aws_lambda_powertools.utilities.typing import LambdaContext
    
    persistence_layer = DynamoDBPersistenceLayer(table_name="ddbidempotency")
    
    @idempotent(persistence_layer=persistence_layer, idempotency_key_custom_prefix="my_other_custom_prefix")
    def lambda_handler(event: dict, context: LambdaContext):
        return "a"
  10. leandrodamascena commented on Jan 23, 2025

    @leandrodamascena
    Contributor

    On second thought, it doesn't make much sense to add idempotency_key to the parameter name, it's implicit that this is part of the idempotency mechanism. I'm refactoring to call the parameter as key_prefix.

  11. github-actions commented on Jan 23, 2025

    @github-actions
    Contributor

    ⚠️COMMENT VISIBILITY WARNING⚠️

    This issue is now closed. Please be mindful that future comments are hard for our team to see.

    If you need more assistance, please either tag a team member or open a new issue that references this one.

    If you wish to keep having a conversation with other community members under this issue feel free to do so.

  12. added
    pending-release
    on Jan 23, 2025
  13. github-actions commented on Jan 28, 2025

    @github-actions
    Contributor

    This is now released under 3.5.0 version!

  14. removed
    pending-release
    on Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature-requestidempotency

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions