Skip to content

Commit df9e8a3

Browse files
committed
docs: recommend process_partial_response and necessary fixes
replace all examples with batch_processor with process_partial_response and its async equivalent. renamed 'As a decorator' code sections to make it explicitly legacy. add new section 'Recommended' using the new behaviour fixed all incorrect Pydantic examples while replacing batch_processor
1 parent 42cda1f commit df9e8a3

9 files changed

+236
-71
lines changed

docs/utilities/batch.md

Lines changed: 93 additions & 68 deletions
Large diffs are not rendered by default.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import json
2+
from typing import Optional
3+
4+
from aws_lambda_powertools import Logger, Tracer
5+
from aws_lambda_powertools.utilities.batch import (
6+
BatchProcessor,
7+
EventType,
8+
process_partial_response,
9+
)
10+
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
11+
from aws_lambda_powertools.utilities.typing import LambdaContext
12+
13+
processor = BatchProcessor(event_type=EventType.SQS)
14+
tracer = Tracer()
15+
logger = Logger()
16+
17+
18+
@tracer.capture_method
19+
def record_handler(record: SQSRecord, lambda_context: Optional[LambdaContext] = None):
20+
payload: str = record.body
21+
if payload:
22+
item: dict = json.loads(payload)
23+
logger.info(item)
24+
...
25+
26+
27+
@logger.inject_lambda_context
28+
@tracer.capture_lambda_handler
29+
def lambda_handler(event, context: LambdaContext):
30+
return process_partial_response(event=event, record_handler=record_handler, processor=processor, context=context)

examples/batch_processing/src/getting_started_async_batch_processor.py renamed to examples/batch_processing/src/getting_started_async.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from aws_lambda_powertools.utilities.batch import (
44
AsyncBatchProcessor,
55
EventType,
6-
async_batch_processor,
6+
async_process_partial_response,
77
)
88
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
99
from aws_lambda_powertools.utilities.typing import LambdaContext
@@ -20,6 +20,7 @@ async def async_record_handler(record: SQSRecord):
2020
return ret.status_code
2121

2222

23-
@async_batch_processor(record_handler=async_record_handler, processor=processor)
2423
def lambda_handler(event, context: LambdaContext):
25-
return processor.response()
24+
return async_process_partial_response(
25+
event=event, record_handler=async_record_handler, processor=processor, context=context
26+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import json
2+
3+
from aws_lambda_powertools import Logger, Tracer
4+
from aws_lambda_powertools.utilities.batch import (
5+
BatchProcessor,
6+
EventType,
7+
process_partial_response,
8+
)
9+
from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import (
10+
DynamoDBRecord,
11+
)
12+
from aws_lambda_powertools.utilities.typing import LambdaContext
13+
14+
processor = BatchProcessor(event_type=EventType.DynamoDBStreams)
15+
tracer = Tracer()
16+
logger = Logger()
17+
18+
19+
@tracer.capture_method
20+
def record_handler(record: DynamoDBRecord):
21+
logger.info(record.dynamodb.new_image)
22+
payload: dict = json.loads(record.dynamodb.new_image.get("Message"))
23+
logger.info(payload)
24+
...
25+
26+
27+
@logger.inject_lambda_context
28+
@tracer.capture_lambda_handler
29+
def lambda_handler(event, context: LambdaContext):
30+
return process_partial_response(event=event, record_handler=record_handler, processor=processor, context=context)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from aws_lambda_powertools import Logger, Tracer
2+
from aws_lambda_powertools.utilities.batch import (
3+
BatchProcessor,
4+
EventType,
5+
process_partial_response,
6+
)
7+
from aws_lambda_powertools.utilities.data_classes.kinesis_stream_event import (
8+
KinesisStreamRecord,
9+
)
10+
from aws_lambda_powertools.utilities.typing import LambdaContext
11+
12+
processor = BatchProcessor(event_type=EventType.KinesisDataStreams)
13+
tracer = Tracer()
14+
logger = Logger()
15+
16+
17+
@tracer.capture_method
18+
def record_handler(record: KinesisStreamRecord):
19+
logger.info(record.kinesis.data_as_text)
20+
payload: dict = record.kinesis.data_as_json()
21+
logger.info(payload)
22+
...
23+
24+
25+
@logger.inject_lambda_context
26+
@tracer.capture_lambda_handler
27+
def lambda_handler(event, context: LambdaContext):
28+
return process_partial_response(event=event, record_handler=record_handler, processor=processor, context=context)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
3+
from aws_lambda_powertools import Logger, Tracer
4+
from aws_lambda_powertools.utilities.batch import (
5+
BatchProcessor,
6+
EventType,
7+
process_partial_response,
8+
)
9+
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
10+
from aws_lambda_powertools.utilities.typing import LambdaContext
11+
12+
processor = BatchProcessor(event_type=EventType.SQS)
13+
tracer = Tracer()
14+
logger = Logger()
15+
16+
17+
@tracer.capture_method
18+
def record_handler(record: SQSRecord):
19+
payload: str = record.body
20+
if payload:
21+
item: dict = json.loads(payload)
22+
logger.info(item)
23+
...
24+
25+
26+
@logger.inject_lambda_context
27+
@tracer.capture_lambda_handler
28+
def lambda_handler(event, context: LambdaContext):
29+
return process_partial_response(event=event, record_handler=record_handler, processor=processor, context=context)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from aws_lambda_powertools import Logger, Tracer
2+
from aws_lambda_powertools.utilities.batch import (
3+
SqsFifoPartialProcessor,
4+
process_partial_response,
5+
)
6+
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
7+
from aws_lambda_powertools.utilities.typing import LambdaContext
8+
9+
processor = SqsFifoPartialProcessor()
10+
tracer = Tracer()
11+
logger = Logger()
12+
13+
14+
@tracer.capture_method
15+
def record_handler(record: SQSRecord):
16+
...
17+
18+
19+
@logger.inject_lambda_context
20+
@tracer.capture_lambda_handler
21+
def lambda_handler(event, context: LambdaContext):
22+
return process_partial_response(event=event, record_handler=record_handler, processor=processor, context=context)

0 commit comments

Comments
 (0)