Upgrading to AWS Lambda Powertools for Python v2

Upgrading to AWS Lambda Powertools for Python v2

The AWS Lambda Powertools for Python make it easier for developers to build high quality Lambda functions. In the three or so years since the v0.1.0 beta dropped back in November 2019, there have been almost releases of the library. In late October the team hit the v2.0.0 milestone. Rather than duplicating all of the release notes, I’ll just cover my personal favourites before getting into the upgrade process.

Powertools v1 supported ARM, but only when installing the library using a python package manager. The official Lambda layer didn’t support ARM. v2 promotes ARM to a first class citizen. This makes it even easier for teams to use Powertools for small, efficient Lambda functions with no additional dependencies.

Python packaging has improved in v2. When installing Powertools v1, it pulled in dependencies needed by all of the features. Now dependencies only required by one or more features are omitted by default. This results in faster install times, including during CI builds.

Tracing now uses fully qualified names. This makes it easier to identify the source of a span in a trace. This small change makes it even easier for developers to quickly interpret traces in X-Ray.

The backwards compatibility breaks are documented in the relnotes. None of the features I rely have changed in this release, which made the upgrade easy.

Unless you’re using a removed or changed feature, the upgrade should be painless.

Upgrade Steps

If you’re using the Lambda layer, the first step is to update your requirements-dev.txt file to use aws-lambda-powertools==2.1.0 . If you’re using the tracing, validation or parser, you will need to include the additional dependencies. If you’re not using the Lambda layer, update your requirements.txt file and skip the layer update instructions below.

Run your tests and deal with any surprises.

The next step is to update your Lambda layer reference. If you’re using Terraform, use the following code:

resource "aws_lambda_function" "my_function" {
  function_name = local.lambda_namespace

  runtime       = "python3.9"
  architectures = ["arm64"] # Give this a try!

  layers = [local.powertools_layer_arn] # Use this approach if you have multiple lambdas in project
  # ...

# ...

locals {
  powertools_layer_arn = "arn:aws:lambda:${local.region}:017000801446:layer:AWSLambdaPowertoolsPythonV2-Arm64:12"
  region               = data.aws_region.current.name

If you’re using AWS SAM, then you can upgrade by making this one line change:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
    # ...
        # ...
            - !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:12

Logging Improvements in v2.1.0

v2.1.0 introduces improvements to the logger. This change allows developers to pass additional metadata to the logger as **kwargs , rather than passing them as a dict to the extra argument.

In the past the timestamps in logs have used the ISO8601 format. A new flag was introduced to opt into using a format that complies with both ISO8601 and RFC3339.

These are both nice developer experience improvements.

The logger change helped me catch an incorrectly implemented test. I was logging a critical message on a failure and my test checked for the call. Instead of checking for a call to the Powertools logger, I was checking for a call to the core Python logger. Until v2.1.0 my tests had always passed. Prior to this version, Powertools didn’t set the optional arguments when calling the core logger. In v2 these arguments are always set. This broke my tests and highlighted the bug.