Skip to content

Commit a1b9215

Browse files
authored
feature: move idempotency to logic function (#731)
1 parent 973339c commit a1b9215

File tree

7 files changed

+143
-338
lines changed

7 files changed

+143
-338
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"dependencies": {
3-
"aws-cdk": "^2.93.0"
3+
"aws-cdk": "^2.95.0"
44
}
55
}

poetry.lock

Lines changed: 123 additions & 328 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/handlers/create_order.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from aws_lambda_env_modeler import get_environment_variables, init_environment_variables
55
from aws_lambda_powertools.metrics import MetricUnit
66
from aws_lambda_powertools.utilities.feature_flags.exceptions import ConfigurationStoreError, SchemaValidationError
7-
from aws_lambda_powertools.utilities.idempotency import idempotent
87
from aws_lambda_powertools.utilities.parser import ValidationError, parse
98
from aws_lambda_powertools.utilities.parser.envelopes import ApiGatewayEnvelope
109
from aws_lambda_powertools.utilities.typing import LambdaContext
@@ -13,7 +12,6 @@
1312
from service.handlers.schemas.env_vars import MyHandlerEnvVars
1413
from service.handlers.utils.dynamic_configuration import parse_configuration
1514
from service.handlers.utils.http_responses import build_response
16-
from service.handlers.utils.idempotency import IDEMPOTENCY_CONFIG, IDEMPOTENCY_LAYER
1715
from service.handlers.utils.observability import logger, metrics, tracer
1816
from service.logic.handle_create_request import handle_create_request
1917
from service.schemas.exceptions import InternalServerException
@@ -23,7 +21,6 @@
2321

2422
@init_environment_variables(model=MyHandlerEnvVars)
2523
@metrics.log_metrics
26-
@idempotent(persistence_store=IDEMPOTENCY_LAYER, config=IDEMPOTENCY_CONFIG)
2724
@tracer.capture_lambda_handler(capture_response=False)
2825
def create_order(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]:
2926
logger.set_correlation_id(context.aws_request_id)
@@ -51,6 +48,7 @@ def create_order(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any
5148
response: CreateOrderOutput = handle_create_request(
5249
order_request=create_input,
5350
table_name=env_vars.TABLE_NAME,
51+
context=context,
5452
)
5553
except InternalServerException: # pragma: no cover
5654
logger.error('finished handling create order request with internal error')

service/logic/handle_create_request.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
from aws_lambda_powertools.utilities.idempotency import idempotent_function
2+
from aws_lambda_powertools.utilities.idempotency.serialization.pydantic import PydanticSerializer
3+
from aws_lambda_powertools.utilities.typing import LambdaContext
4+
15
from service.dal.db_handler import DalHandler
26
from service.dal.dynamo_dal_handler import get_dal_handler
37
from service.dal.schemas.db import OrderEntry
48
from service.handlers.schemas.dynamic_configuration import FeatureFlagsNames
59
from service.handlers.utils.dynamic_configuration import get_dynamic_configuration_store
610
from service.handlers.utils.observability import logger, tracer
11+
from service.logic.utils.idempotency import IDEMPOTENCY_CONFIG, IDEMPOTENCY_LAYER
712
from service.schemas.input import CreateOrderRequest
813
from service.schemas.output import CreateOrderOutput
914

1015

16+
@idempotent_function(
17+
data_keyword_argument='order_request',
18+
config=IDEMPOTENCY_CONFIG,
19+
persistence_store=IDEMPOTENCY_LAYER,
20+
output_serializer=PydanticSerializer,
21+
)
1122
@tracer.capture_method(capture_response=False)
12-
def handle_create_request(order_request: CreateOrderRequest, table_name: str) -> CreateOrderOutput:
23+
def handle_create_request(order_request: CreateOrderRequest, table_name: str, context: LambdaContext) -> CreateOrderOutput:
24+
IDEMPOTENCY_CONFIG.register_lambda_context(context) # see Lambda timeouts section
25+
1326
logger.info('starting to handle create request', extra={
1427
'order_item_count': order_request.order_item_count,
1528
'customer_name': order_request.customer_name

service/logic/utils/__init__.py

Whitespace-only changes.

service/handlers/utils/idempotency.py renamed to service/logic/utils/idempotency.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
IDEMPOTENCY_LAYER = DynamoDBPersistenceLayer(table_name=get_environment_variables(model=Idempotency).IDEMPOTENCY_TABLE_NAME)
77
IDEMPOTENCY_CONFIG = IdempotencyConfig(
88
expires_after_seconds=5 * 60, # 5 minutes
9-
event_key_jmespath='powertools_json(body).[customer_name, order_item_count]',
109
)

0 commit comments

Comments
 (0)