Skip to content

Commit f380f57

Browse files
committed
docs: add migration guide
1 parent 580eeae commit f380f57

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

docs/utilities/batch.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,102 @@ When using Sentry.io for error monitoring, you can override `failure_handler` to
10571057

10581058
## Legacy
10591059

1060-
!!! tip "This is kept for historical purposes. Use the new [BatchProcessor](#processing-messages-from-sqs) instead. "
1060+
!!! tip "This is kept for historical purposes. Use the new [BatchProcessor](#processing-messages-from-sqs) instead."
1061+
1062+
1063+
### Migration guide
1064+
1065+
!!! info "keep reading if you are using `sqs_batch_processor` or `PartialSQSProcessor`"
1066+
1067+
[As of Nov 2021](https://aws.amazon.com/about-aws/whats-new/2021/11/aws-lambda-partial-batch-response-sqs-event-source/){target="_blank"}, this is no longer needed as both SQS, Kinesis, and DynamoDB Streams offer this capability natively with one caveat - it's an [opt-in feature](#required-resources).
1068+
1069+
Being a native feature, we no longer need to instantiate boto3 nor other customizations like exception suppressing – this lowers the cost of your Lambda function as you can delegate deleting partial failures to Lambda.
1070+
1071+
!!! tip "It's also easier to test since it's mostly a [contract based response](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#sqs-batchfailurereporting-syntax){target="_blank"}."
1072+
1073+
You can migrate in three steps:
1074+
1075+
1. If you are using **`sqs_batch_decorator`** you can now use **`batch_processor`** decorator
1076+
2. If you were using **`PartialSQSProcessor`** you can now use **`BatchProcessor`**
1077+
3. Change your Lambda Handler to return the new response format
1078+
1079+
1080+
=== "Decorator: Before"
1081+
1082+
```python hl_lines="1 6"
1083+
from aws_lambda_powertools.utilities.batch import sqs_batch_processor
1084+
1085+
def record_handler(record):
1086+
return do_something_with(record["body"])
1087+
1088+
@sqs_batch_processor(record_handler=record_handler)
1089+
def lambda_handler(event, context):
1090+
return {"statusCode": 200}
1091+
```
1092+
1093+
=== "Decorator: After"
1094+
1095+
```python hl_lines="3 5 11"
1096+
import json
1097+
1098+
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, batch_processor
1099+
1100+
processor = BatchProcessor(event_type=EventType.SQS)
1101+
1102+
1103+
def record_handler(record):
1104+
return do_something_with(record["body"])
1105+
1106+
@batch_processor(record_handler=record_handler, processor=processor)
1107+
def lambda_handler(event, context):
1108+
return processor.response()
1109+
```
1110+
1111+
1112+
=== "Context manager: Before"
1113+
1114+
```python hl_lines="1-2 4 14 19"
1115+
from aws_lambda_powertools.utilities.batch import PartialSQSProcessor
1116+
from botocore.config import Config
1117+
1118+
config = Config(region_name="us-east-1")
1119+
1120+
def record_handler(record):
1121+
return_value = do_something_with(record["body"])
1122+
return return_value
1123+
1124+
1125+
def lambda_handler(event, context):
1126+
records = event["Records"]
1127+
1128+
processor = PartialSQSProcessor(config=config)
1129+
1130+
with processor(records, record_handler):
1131+
result = processor.process()
1132+
1133+
return result
1134+
```
1135+
1136+
=== "Context manager: After"
1137+
1138+
```python hl_lines="1 11"
1139+
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, batch_processor
1140+
1141+
1142+
def record_handler(record):
1143+
return_value = do_something_with(record["body"])
1144+
return return_value
1145+
1146+
def lambda_handler(event, context):
1147+
records = event["Records"]
1148+
1149+
processor = BatchProcessor(event_type=EventType.SQS)
1150+
1151+
with processor(records, record_handler):
1152+
result = processor.process()
1153+
1154+
return processor.response()
1155+
```
10611156

10621157
### Customizing boto configuration
10631158

0 commit comments

Comments
 (0)