pypi aws-lambda-powertools 2.0.0
v2.0.0

latest releases: 2.40.2a0, 2.40.1, 2.40.1a1...
20 months ago

Summary

We are extremely happy to announce the smallest release version ever - v2.0.0 ๐ŸŽ‰๐ŸŽ‰!

The number one customer request was reducing package size. Some customers use a different tracing provider. Others used Parser (Pydantic) for data modelling and deep data validation instead of JSON Schema Validation. A few brought large machine learning models and every bit of space they can save matters.

The number two request was ARM64 support. Customers want to benefit from performance and cost savings for their workloads. However, they donโ€™t want to invest additional time testing and compiling dependencies they use daily for ARM64.

Weโ€™ve spent the past 2 months optimizing as much as we could. We kept breaking changes to a minimum to make your transition to v2 as smooth as possible.

For backwards incompatible changes, weโ€™ve spent considerable effort making sure they are visible, why we implemented them, and and what to do if they affect you.

Our sincerest appreciation for the community help and patience while weโ€™ve worked on this release - Thank you โค๏ธ!


Table of contents

  • What's New in V2
    • Python compatibility
    • Package size reduction by 97.6%
      • PyPi
      • Lambda Layer
    • ARM Support
    • PyPi and Lambda Layer available upon release notes event
    • Event Handler REST new features
      • Multi-value header and cookies support
      • Trailing slash route support
      • Fully qualified names for Tracer subsegments
    • Backwards incompatible change in v2
      • Event Source Data Classes DynamoDBStreamEvent
      • Feature Flags and Parameters AppConfig new IAM permission
      • Idempotency partition key format
    • Deprecated features removed
      • Legacy SQS Batch Processor
      • POWERTOOLS_EVENT_HANDLER_DEBUG environment variable
  • Changes
  • ๐ŸŒŸNew features and non-breaking changes
  • ๐Ÿ“œ Documentation updates
  • ๐Ÿ› Bug and hot fixes
  • ๐Ÿ”ง Maintenance
  • This release was made possible by the following contributors:

What's New in V2

Python compatibility

We dropped support for Python 3.6. We highly recommend moving to the latest Python available in AWS Lambda (3.9).

Package size reduction by 97.6%

We now rely on AWS SDK available in the Lambda runtime, and all other dependencies are marked as optional.

There are subtle but important differences in the installation experience - PyPi (all optional) and Lambda Layer (all included).

PyPi

The compressed package size is now approximately 240K (previously 10M).

We updated each documentation to emphasize when an additional dependency is required (Tracer, Validation and Parser). If you prefer to install all of them, use pip install aws-lambda-powertools[all].

Note. If you need to use newer AWS services or features not available in the Lambda runtime provided SDK, you'll need to bundle boto3 as a dependency.

Lambda Layer

The compressed package size is now approximately 2M (previously 17M).

Lambda Layer includes all dependencies. They're also optimized for package size and speed (Pydantic now compiled with Cython for ~30-50% speed gain).

ARM Support

We now offer full support to ARM via Lambda Layer and SAR installation. All C-extension dependencies are compiled, optimized and tested against Lambda environment.

PyPi and Lambda Layer available upon release notes event

You can now safely subscribe to GitHub Release event to know when PyPi and Lambda Layer ARNs have the latest version.

Previously, PyPi, Lambda Layers, and docs were updated after a release notes went live to GitHub (~15m completion). Due to Lambda Layer compilation (x86/ARM64), this process takes on average 30m longer.

As such, we make releases prior to sharing release notes, guaranteeing for customers watching for GitHub Releases that Layer ARNs available in the documentation match PyPi releases (no more delays).

Event Handler REST new features

Multi-value header and cookies support

When returning custom responses, you now can add a list of values for a header, including cookies.

Event Handler will seamless adapt to the expected format based on resolver used, whether that is REST API, HTTP API, ALB, or Function URL.

from http import HTTPStatus
from uuid import uuid4

import requests

from aws_lambda_powertools.event_handler import (
    APIGatewayRestResolver,
    Response,
    content_types,
)
from aws_lambda_powertools.shared.cookies import Cookie
from aws_lambda_powertools.utilities.typing import LambdaContext

app = APIGatewayRestResolver()


@app.get("/todos")
def get_todos():
    todos: requests.Response = requests.get("https://jsonplaceholder.typicode.com/todos")
    todos.raise_for_status()

    dummy_transactions = [f"{uuid4()}", f"{uuid4()}", f"{uuid4()}"]
    multi_value_header = {"X-Transaction-Id": dummy_transactions}

    return Response(
        status_code=HTTPStatus.OK.value,  # 200
        content_type=content_types.APPLICATION_JSON,
        body=todos.json()[:10],
        headers=multi_value_header,  # NEW
        cookies=[Cookie(name="session_id", value="12345")],  # NEW
    )

def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)

NOTE. For ALB, you have to explicitly enable multi-value headers in the target configuration.

Trailing slash route support

APIGatewayRestResolver now seamless support route resolution with trailing slashes: /todos/.

Previously, requests with a trailing slash would return HTTP 404 due to route mismatch.

Note. This was not necessary for HTTP API and ALB.

Fully qualified names for Tracer subsegments

tracer.capture_method or tracer.capture_lambda_handler decorators now use the decorated function or method fully qualified name as the subsegment name.

This enables accurate traces for ABC or Protocol classes, where their method name will be the same but for distinct classes.

image

Backwards incompatible change in v2

Hereโ€™s a quick view of deprecated features removed and backwards incompatible changes.

Area Change Code change required IAM Permissions change required
Batch Removed legacy SQS batch processor in favour of BatchProcessor. Yes -
Environment variables Removed legacy POWERTOOLS_EVENT_HANDLER_DEBUG in favour of POWERTOOLS_DEV. - -
Event Handler Updated headers response format due to multi-value headers and cookie support. Tests only -
Event Source Data Classes Replaced DynamoDBStreamEvent AttributeValue with native Python types. Yes -
Feature Flags / Parameters Updated AppConfig API calls due to GetConfiguration API deprecation. - Yes
Idempotency Updated partition key to include fully qualified function/method names. - -

Event Source Data Classes DynamoDBStreamEvent

Upgrade guide

You will now receive native Python types when accessing DynamoDB records via keys, new_image, and old_image attributes in DynamoDBStreamEvent.

Previously, you'd receive an AttributeValue instance. For most customers, this made it difficult to handle Change Data Capture use cases due to having an additional step to deserialize data to Python native types (str, dict etc) when processing them.

from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import (
    DynamoDBStreamEvent,
    DynamoDBRecordEventName
)

def send_to_sqs(data: Dict):
    body = json.dumps(data)
    ...

@event_source(data_class=DynamoDBStreamEvent)
def lambda_handler(event: DynamoDBStreamEvent, context):
    for record in event.records:

        # BEFORE
        new_image: Dict[str, AttributeValue] = record.dynamodb.new_image
        event_type: AttributeValue = new_image["eventType"].get_value
        if event_type == "PENDING":
            # deserialize attribute value into Python native type
            # NOTE: nested objects would need additional logic
            data = {k: v.get_value for k, v in image.items()}
            send_to_sqs(data)

        # AFTER
        new_image: Dict[str, Any] = record.dynamodb.new_image
        if new_image.get("eventType") == "PENDING":
            send_to_sqs(new_image)  # Here new_image is just a Python Dict type

Feature Flags and Parameters AppConfig new IAM permission

Upgrade guide. No code changes required.

We replaced GetConfiguration API (now deprecated) with GetLatestConfiguration and StartConfigurationSession.

As such, you must update your IAM Role permissions to allow the following IAM actions:

  • appconfig:GetLatestConfiguration
  • appconfig:StartConfigurationSession

Idempotency partition key format

Upgrade guide. No code changes required

Similar to Tracer, we also updated Idempotency to use fully qualified name. This means that recent non-expired idempotent transactions will be ignored.

image

Deprecated features removed

Legacy SQS Batch Processor

Upgrade guide.

We removed the deprecated PartialSQSProcessor class and sqs_batch_processor decorator in favour of BatchProcessor launched 11 months ago.

BatchProcessor provides nearly the same developer experience while improving speed and security, as it natively integrates with Lambda ReportBachItemFailures feature.

POWERTOOLS_EVENT_HANDLER_DEBUG environment variable

We removed the POWERTOOLS_EVENT_HANDLER_DEBUG environment variable in favour of POWERTOOLS_DEV.

POWERTOOLS_DEV consolidate additional features to ease prototyping against local or non-production environment.

Changes

๐ŸŒŸNew features and non-breaking changes

  • feat(ci): release docs as alpha when doing a pre-release (#1624) by @rubenfonseca
  • feat(data-classes): replace AttributeValue in DynamoDBStreamEvent with deserialized Python values (#1619) by @shanab
  • feat(apigateway): ignore trailing slashes in routes (APIGatewayRestResolver) (#1609) by @walmsles
  • docs(homepage): auto-update Layer ARN on every release (#1610) by @rubenfonseca
  • feat(data_classes): add KinesisFirehoseEvent (#1540) by @ryandeivert

๐Ÿ“œ Documentation updates

  • fix(ci): remove v2 suffix from SAR apps (#1633) by @rubenfonseca
  • docs(upgrade_guide): add latest changes and quick summary (#1623) by @leandrodamascena
  • refactor(apigateway): remove POWERTOOLS_EVENT_HANDLER_DEBUG env var (#1620) by @heitorlessa
  • feat(data-classes): replace AttributeValue in DynamoDBStreamEvent with deserialized Python values (#1619) by @shanab
  • feat(apigateway): ignore trailing slashes in routes (APIGatewayRestResolver) (#1609) by @walmsles
  • docs(homepage): auto-update Layer ARN on every release (#1610) by @rubenfonseca
  • feat(data_classes): add KinesisFirehoseEvent (#1540) by @ryandeivert

๐Ÿ› Bug and hot fixes

๐Ÿ”ง Maintenance

This release was made possible by the following contributors:

@barreeeiroo, @dependabot, @dependabot[bot], @heitorlessa, @kt-hr, @leandrodamascena, @rubenfonseca, @ryandeivert, @shanab, @walmsles, @ran-isenberg and Release bot

Don't miss a new aws-lambda-powertools release

NewReleases is sending notifications on new releases.