Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Idempotency prefix is susceptible to refactoring #5897

Closed
2 tasks done
lbustelo opened this issue Jan 22, 2025 · 7 comments · Fixed by #5898
Closed
2 tasks done

Feature request: Idempotency prefix is susceptible to refactoring #5897

lbustelo opened this issue Jan 22, 2025 · 7 comments · Fixed by #5898
Assignees
Labels
feature-request feature request idempotency Idempotency utility

Comments

@lbustelo
Copy link

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

@lbustelo lbustelo added feature-request feature request triage Pending triage from maintainers labels Jan 22, 2025
Copy link

boring-cyborg bot commented Jan 22, 2025

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

@dreamorosi
Copy link
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.

@dreamorosi dreamorosi moved this from Triage to Ideas in Powertools for AWS Lambda (Python) Jan 22, 2025
@dreamorosi dreamorosi removed the triage Pending triage from maintainers label Jan 22, 2025
@dreamorosi dreamorosi moved this from Ideas to Pending review in Powertools for AWS Lambda (Python) Jan 22, 2025
@am29d
Copy link
Contributor

am29d commented Jan 22, 2025

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.

@leandrodamascena
Copy link
Contributor

leandrodamascena commented Jan 22, 2025

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"

@leandrodamascena leandrodamascena moved this from Ideas to Pending review in Powertools for AWS Lambda (Python) Jan 22, 2025
@leandrodamascena leandrodamascena added the idempotency Idempotency utility label Jan 22, 2025
@leandrodamascena
Copy link
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.

Copy link
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.

Copy link
Contributor

This is now released under 3.5.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request feature request idempotency Idempotency utility
Projects
Status: Shipped
4 participants