@@ -273,7 +273,7 @@ Use the context manager to access a list of all returned values from your `recor
273
273
* ** When successful** . We will include a tuple with ` success ` , the result of ` record_handler ` , and the batch record
274
274
* ** When failed** . We will include a tuple with ` fail ` , exception as a string, and the batch record
275
275
276
- ``` python hl_lines="30-36 " title="Accessing processed messages via context manager"
276
+ ``` python hl_lines="26-34 " title="Accessing processed messages via context manager"
277
277
-- 8 < -- " examples/batch_processing/src/context_manager_access.py"
278
278
```
279
279
@@ -291,63 +291,14 @@ We can automatically inject the [Lambda context](https://docs.aws.amazon.com/lam
291
291
292
292
=== "As a decorator (legacy)"
293
293
294
- ```python hl_lines="15"
295
- from typing import Optional
296
-
297
- from aws_lambda_powertools import Logger, Tracer
298
- from aws_lambda_powertools.utilities.batch import (BatchProcessor, EventType,
299
- batch_processor)
300
- from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
301
- from aws_lambda_powertools.utilities.typing import LambdaContext
302
-
303
- processor = BatchProcessor(event_type=EventType.SQS)
304
- tracer = Tracer()
305
- logger = Logger()
306
-
307
-
308
- @tracer.capture_method
309
- def record_handler(record: SQSRecord, lambda_context: Optional[LambdaContext] = None):
310
- if lambda_context is not None:
311
- remaining_time = lambda_context.get_remaining_time_in_millis()
312
- ...
313
-
314
-
315
- @logger.inject_lambda_context
316
- @tracer.capture_lambda_handler
317
- @batch_processor(record_handler=record_handler, processor=processor)
318
- def lambda_handler(event, context: LambdaContext):
319
- return processor.response()
294
+ ```python hl_lines="18"
295
+ --8<-- "examples/batch_processing/src/advanced_accessing_lambda_context_decorator.py"
320
296
```
321
297
322
298
=== "As a context manager"
323
299
324
- ```python hl_lines="14 23"
325
- from typing import Optional
326
-
327
- from aws_lambda_powertools import Logger, Tracer
328
- from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType
329
- from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
330
- from aws_lambda_powertools.utilities.typing import LambdaContext
331
-
332
- processor = BatchProcessor(event_type=EventType.SQS)
333
- tracer = Tracer()
334
- logger = Logger()
335
-
336
-
337
- @tracer.capture_method
338
- def record_handler(record: SQSRecord, lambda_context: Optional[LambdaContext] = None):
339
- if lambda_context is not None:
340
- remaining_time = lambda_context.get_remaining_time_in_millis()
341
- ...
342
-
343
- @logger.inject_lambda_context
344
- @tracer.capture_lambda_handler
345
- def lambda_handler(event, context: LambdaContext):
346
- batch = event["Records"]
347
- with processor(records=batch, handler=record_handler, lambda_context=context):
348
- result = processor.process()
349
-
350
- return result
300
+ ```python hl_lines="14 24"
301
+ --8<-- "examples/batch_processing/src/advanced_accessing_lambda_context_manager.py"
351
302
```
352
303
353
304
### Extending BatchProcessor
0 commit comments