Upgrade guide
Migrate to v2 from v1
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.
Initial Steps
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
Event Handler Response (headers and cookies)
The Response
class of the event handler utility changed slightly:
- The
headers
parameter now expects either a value or list of values per header (type Union[str, Dict[str, List[str]]]
)
- We introduced a new
cookies
parameter (type List[str]
)
Note
Code that set headers as Dict[str, str]
will still work unchanged.
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | @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)],
)
|
Legacy SQS Batch Processor
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 use batch_processor
decorator
- If you use
PartialSQSProcessor
you can now use BatchProcessor
- Enable the functionality on SQS
- Change your Lambda Handler to return the new response format
| 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}
|
1
2
3
4
5
6
7
8
9
10
11
12
13 | 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()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | 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()
|
Last update:
2022-09-02