Skip to content

Commit 7b06ae2

Browse files
committed
docs(batch): document the new lambda context feature
1 parent 90347a3 commit 7b06ae2

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

docs/utilities/batch.md

+67
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,73 @@ def lambda_handler(event, context: LambdaContext):
833833
return processor.response()
834834
```
835835

836+
### Accessing Lambda Context
837+
838+
Within your `record_handler` function, you might need access to the Lambda context to determine how much time you have left before your function times out.
839+
840+
We can automatically inject the [Lambda context](https://docs.aws.amazon.com/lambda/latest/dg/python-context.html){target="_blank"} into your `record_handler` if your function signature has a parameter named `lambda_context`. When using a context manager, you also need to pass the Lambda context object like in the example below.
841+
842+
=== "As a decorator"
843+
844+
```python hl_lines="15"
845+
from typing import Optional
846+
847+
from aws_lambda_powertools import Logger, Tracer
848+
from aws_lambda_powertools.utilities.batch import (BatchProcessor, EventType,
849+
batch_processor)
850+
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
851+
from aws_lambda_powertools.utilities.typing import LambdaContext
852+
853+
processor = BatchProcessor(event_type=EventType.SQS)
854+
tracer = Tracer()
855+
logger = Logger()
856+
857+
858+
@tracer.capture_method
859+
def record_handler(record: SQSRecord, lambda_context: Optional[LambdaContext] = None):
860+
if lambda_context is not None:
861+
remaining_time = lambda_context.get_remaining_time_in_millis()
862+
...
863+
864+
865+
@logger.inject_lambda_context
866+
@tracer.capture_lambda_handler
867+
@batch_processor(record_handler=record_handler, processor=processor)
868+
def lambda_handler(event, context: LambdaContext):
869+
return processor.response()
870+
```
871+
872+
=== "As a context manager"
873+
874+
```python hl_lines="14 23"
875+
from typing import Optional
876+
877+
from aws_lambda_powertools import Logger, Tracer
878+
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType
879+
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
880+
from aws_lambda_powertools.utilities.typing import LambdaContext
881+
882+
processor = BatchProcessor(event_type=EventType.SQS)
883+
tracer = Tracer()
884+
logger = Logger()
885+
886+
887+
@tracer.capture_method
888+
def record_handler(record: SQSRecord, lambda_context: Optional[LambdaContext] = None):
889+
if lambda_context is not None:
890+
remaining_time = lambda_context.get_remaining_time_in_millis()
891+
...
892+
893+
@logger.inject_lambda_context
894+
@tracer.capture_lambda_handler
895+
def lambda_handler(event, context: LambdaContext):
896+
batch = event["Records"]
897+
with processor(records=batch, handler=record_handler, lambda_context=context):
898+
result = processor.process()
899+
900+
return result
901+
```
902+
836903
### Extending BatchProcessor
837904

838905
You might want to bring custom logic to the existing `BatchProcessor` to slightly override how we handle successes and failures.

0 commit comments

Comments
 (0)