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

feat(idempotency): add support for custom Idempotency key prefix #5898

Merged
merged 3 commits into from
Jan 23, 2025

Conversation

leandrodamascena
Copy link
Contributor

@leandrodamascena leandrodamascena commented Jan 22, 2025

Issue number: #5897

Summary

Changes

This PR introduces an optional idempotency_key_custom_prefix parameter to the @idempotent_function and @idempotent decorators, allowing customers more flexibility in managing idempotency key generation.

Key points for this decision
Currently, idempotency keys are automatically generated based on the function's name and arguments. This can create challenges during:

  • Code refactoring
  • Different workloads with same name function and expect to receive same kind of payload

User experience

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,
    key_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, key_prefix="my_other_custom_prefix")
def lambda_handler(event: dict, context: LambdaContext):
    return "a"

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@leandrodamascena leandrodamascena requested a review from a team as a code owner January 22, 2025 20:09
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation tests labels Jan 22, 2025
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 22, 2025
@github-actions github-actions bot added feature New feature or functionality and removed documentation Improvements or additions to documentation labels Jan 22, 2025
@leandrodamascena leandrodamascena linked an issue Jan 22, 2025 that may be closed by this pull request
2 tasks
Copy link

codecov bot commented Jan 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.20%. Comparing base (54f9738) to head (08ade89).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5898      +/-   ##
===========================================
- Coverage    96.20%   96.20%   -0.01%     
===========================================
  Files          232      232              
  Lines        10956    10955       -1     
  Branches      2027     2026       -1     
===========================================
- Hits         10540    10539       -1     
  Misses         327      327              
  Partials        89       89              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@leandrodamascena leandrodamascena added the idempotency Idempotency utility label Jan 22, 2025
@leandrodamascena leandrodamascena self-assigned this Jan 22, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 23, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 23, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 23, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 23, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 23, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 23, 2025
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick turnaround, looks good.

Will open an issue in TypeScript as well!

@leandrodamascena leandrodamascena merged commit 792892e into develop Jan 23, 2025
18 checks passed
@leandrodamascena leandrodamascena deleted the idempotency/prefix-key branch January 23, 2025 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality idempotency Idempotency utility size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Idempotency prefix is susceptible to refactoring
2 participants