title | description |
---|---|
Upgrade guide |
Guide to update between major Powertools versions |
The transition from Powertools for Python v1 to v2 is as painless as possible, as we aimed for minimal breaking changes. Changes at a glance:
- The API for event handler's
Response
has minor changes to support multi value headers and cookies. - The legacy SQS batch processor was removed.
???+ important Powertools for Python v2 drops suport for Python 3.6, following the Python 3.6 End-Of-Life (EOL) reached on December 23, 2021.
Before you start, we suggest making a copy of your current working project or create a new branch with git.
-
Upgrade Python to at least v3.7
-
Ensure you have the latest
aws-lambda-powertools
pip install aws-lambda-powertools -U
-
Review the following sections to confirm whether they affect your code
The Response
class of the event handler utility changed slightly:
- The
headers
parameter now expects either a value or list of values per header (typeUnion[str, Dict[str, List[str]]]
) - We introduced a new
cookies
parameter (typeList[str]
)
???+ note
Code that set headers as Dict[str, str]
will still work unchanged.
@app.get("/todos")
def get_todos():
# Before
return Response(
# ...
headers={"Content-Type": "text/plain"}
)
# After
return Response(
# ...
headers={"Content-Type": ["text/plain"]},
cookies=[Cookie(name="session_id", value="12345", secure=True, http_only=True)],
)
The deprecated PartialSQSProcessor
and sqs_batch_processor
were removed.
You can migrate to the native batch processing capability by:
- If you use
sqs_batch_decorator
you can now usebatch_processor
decorator - If you use
PartialSQSProcessor
you can now useBatchProcessor
- Enable the functionality on SQS
- Change your Lambda Handler to return the new response format
=== "Decorator: Before"
```python hl_lines="1 6"
from aws_lambda_powertools.utilities.batch import sqs_batch_processor
def record_handler(record):
return do_something_with(record["body"])
@sqs_batch_processor(record_handler=record_handler)
def lambda_handler(event, context):
return {"statusCode": 200}
```
=== "Decorator: After"
```python hl_lines="3 5 11"
import json
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, batch_processor
processor = BatchProcessor(event_type=EventType.SQS)
def record_handler(record):
return do_something_with(record["body"])
@batch_processor(record_handler=record_handler, processor=processor)
def lambda_handler(event, context):
return processor.response()
```
=== "Context manager: Before"
```python hl_lines="1-2 4 14 19"
from aws_lambda_powertools.utilities.batch import PartialSQSProcessor
from botocore.config import Config
config = Config(region_name="us-east-1")
def record_handler(record):
return_value = do_something_with(record["body"])
return return_value
def lambda_handler(event, context):
records = event["Records"]
processor = PartialSQSProcessor(config=config)
with processor(records, record_handler):
result = processor.process()
return result
```
=== "Context manager: After"
```python hl_lines="1 11"
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, batch_processor
def record_handler(record):
return_value = do_something_with(record["body"])
return return_value
def lambda_handler(event, context):
records = event["Records"]
processor = BatchProcessor(event_type=EventType.SQS)
with processor(records, record_handler):
result = processor.process()
return processor.response()
```