|
1 |
| -from aws_lambda_powertools import Logger |
2 |
| -from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType |
| 1 | +import os |
| 2 | +from typing import Any, Dict |
| 3 | + |
| 4 | +from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response |
3 | 5 | from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
|
4 | 6 | from aws_lambda_powertools.utilities.idempotency import (
|
5 | 7 | DynamoDBPersistenceLayer,
|
|
8 | 10 | )
|
9 | 11 | from aws_lambda_powertools.utilities.typing import LambdaContext
|
10 | 12 |
|
11 |
| -logger = Logger() |
12 | 13 | processor = BatchProcessor(event_type=EventType.SQS)
|
13 | 14 |
|
14 |
| -dynamodb = DynamoDBPersistenceLayer(table_name="IdempotencyTable") |
15 |
| -config = IdempotencyConfig( |
16 |
| - event_key_jmespath="messageId", # see Choosing a payload subset section |
17 |
| -) |
| 15 | +table = os.getenv("IDEMPOTENCY_TABLE", "") |
| 16 | +dynamodb = DynamoDBPersistenceLayer(table_name=table) |
| 17 | +config = IdempotencyConfig(event_key_jmespath="messageId") |
18 | 18 |
|
19 | 19 |
|
20 | 20 | @idempotent_function(data_keyword_argument="record", config=config, persistence_store=dynamodb)
|
21 | 21 | def record_handler(record: SQSRecord):
|
22 | 22 | return {"message": record.body}
|
23 | 23 |
|
24 | 24 |
|
25 |
| -def lambda_handler(event: SQSRecord, context: LambdaContext): |
| 25 | +def lambda_handler(event: Dict[str, Any], context: LambdaContext): |
26 | 26 | config.register_lambda_context(context) # see Lambda timeouts section
|
27 | 27 |
|
28 |
| - # with Lambda context registered for Idempotency |
29 |
| - # we can now kick in the Bach processing logic |
30 |
| - batch = event["Records"] |
31 |
| - with processor(records=batch, handler=record_handler): |
32 |
| - # in case you want to access each record processed by your record_handler |
33 |
| - # otherwise ignore the result variable assignment |
34 |
| - processed_messages = processor.process() |
35 |
| - logger.info(processed_messages) |
36 |
| - |
37 |
| - return processor.response() |
| 28 | + return process_partial_response( |
| 29 | + event=event, |
| 30 | + context=context, |
| 31 | + processor=processor, |
| 32 | + record_handler=record_handler, |
| 33 | + ) |
0 commit comments