Skip to content

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.

  1. Upgrade Python to at least v3.7

  2. Ensure you have the latest aws-lambda-powertools

    1
    pip install aws-lambda-powertools -U
    
  3. 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:

  1. The headers parameter now expects either a value or list of values per header (type Union[str, Dict[str, List[str]]])
  2. 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:

  1. If you use sqs_batch_decorator you can now use batch_processor decorator
  2. If you use PartialSQSProcessor you can now use BatchProcessor
  3. Enable the functionality on SQS
  4. Change your Lambda Handler to return the new response format
1
2
3
4
5
6
7
8
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