-
Notifications
You must be signed in to change notification settings - Fork 128
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: ability to specify JMESPath custom functions for Idempotency #2368
Comments
Hello @dreamorosi! I think this might cover some user cases where the customer might need to decode a compressed string and use as the idempotent key, or want to add some information to the from dataclasses import dataclass
from aws_lambda_powertools.utilities.idempotency import (
DynamoDBPersistenceLayer,
IdempotencyConfig,
idempotent_function,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from jmespath.functions import signature
from aws_lambda_powertools.utilities.jmespath_utils import (
PowertoolsFunctions
)
class CustomFunctions(PowertoolsFunctions):
# only decode if value is a string
# see supported data types: https://jmespath.org/specification.html#built-in-functions
@signature({"types": ["string"]})
def _func_custom_transform(self, payload: str):
print("Custom function invoked")
return "customfunction"
custom_jmespath_options = {"custom_functions": CustomFunctions()}
dynamodb = DynamoDBPersistenceLayer(table_name="ddbidempotency")
config = IdempotencyConfig(event_key_jmespath="custom_transform(order_id)", jmespath_options=custom_jmespath_options) # see Choosing a payload subset section
@dataclass
class OrderItem:
sku: str
description: str
@dataclass
class Order:
item: OrderItem
order_id: str
@idempotent_function(data_keyword_argument="order", config=config, persistence_store=dynamodb)
def process_order(order: Order):
return f"processed order {order.order_id}"
def lambda_handler(event: dict, context: LambdaContext):
config.register_lambda_context(context) # see Lambda timeouts section
order_item = OrderItem(sku="fake", description="sample")
order = Order(item=order_item, order_id="testid")
# `order` parameter must be called as a keyword argument to work
process_order(order=order) Thanks |
Thank you Leo, we'll add this to the backlog. I'm also adding the |
Use case
When working with the JMESPath expressions, the Idempotency utility uses some custom functions (i.e.
powertools_json()
) that extend the JMESPath built-in functions. This allows customers to work with some complex types that are common when working with AWS payloads.The underlying JMESPath utility used by Idempotency however allows for further customization by allowing customers to set additional custom functions in addition or instead of the Powertools-provided ones.
It would be great if customers were able to pass their own subclass of
Functions
orPowertoolsFunctions
when using the Idempotency utility.Solution/User Experience
Currently the Idempotency utility creates its own instance of the
PowertoolsFunctions
class when instantiating theIdempotencyConfig
class. This allows the various components of the utility to reuse it across the implementation.The setting could be exposed to customers by adding a new option to the
IdempotencyConfig
class/object:Alternative solutions
No response
Acknowledgment
Future readers
Please react with 👍 and your use case to help us understand customer demand.
The text was updated successfully, but these errors were encountered: