github aws-powertools/powertools-lambda-python v3.41.0

59 minutes ago

Summary

This release graduates the Auth utility out of alpha and adds SigV4 request signing to it and closes the nine Idempotency bugs from our audit.

The community carried this one too: sixteen community PRs from twelve people, nine of them contributing for the first time. Welcome @lechakrawarthy, @coughlmao, @sumedh-aerram, @Neeraj111010, @AP24110011695, @haeganm, @CJstate, @krishbuilds01 and @dhairyakhandelwal880-dotcom. Thanks to @soujasK, @Manishram-ai and @anishmehta24 for coming back, @anishmehta24 for the third release in a row.

Reminder: we stop supporting Python 3.10 on February 1, 2027. Details in #8735.

Auth is GA, and auth_alpha is gone

Docs

We moved the Auth utility from aws_lambda_powertools.utilities.auth_alpha to aws_lambda_powertools.utilities.auth. Nothing else changed: JWTVerifier, OAuth2Client, the Event Handler middleware, the API Gateway authorizer and the jwt extra work exactly as in 3.40.0. The alpha label said the API could change before GA; the API held, the import path did not, and we removed the old namespace on purpose so nobody keeps shipping code against it. If you import from auth_alpha, update one line:

from aws_lambda_powertools.utilities.auth import JWTVerifier, OAuth2Client

SigV4 request signing

Docs

You can now sign HTTP requests to IAM-protected AWS endpoints: API Gateway with IAM auth, Lambda Function URLs, AppSync, OpenSearch, S3. SigV4Signer uses the botocore the Lambda runtime already ships, so there is nothing to install. Call sign() and send the result with any HTTP client, or pass requests_auth() or httpx_auth() as auth= and let the client sign every request on the way out. Use service="s3" for S3 and the signer applies the S3 canonical rules. For multi-region signatures, SigV4aSigner lives behind the auth-sigv4a extra.

import requests
from aws_lambda_powertools.utilities.auth import SigV4Signer

api = SigV4Signer(service="execute-api")  # region and credentials come from the Lambda environment
response = requests.post(url, json=payload, auth=api.requests_auth())

This follows the design Powertools for TypeScript shipped this year and closes #2493, open since 2023. Thank you @stephenbawks for the original PR back then.

Environment variables no longer crash on bad values

Set POWERTOOLS_DEV, POWERTOOLS_DEBUG, POWERTOOLS_METRICS_DISABLED, POWERTOOLS_CIRCUIT_BREAKER_DISABLED, POWERTOOLS_IDEMPOTENCY_DISABLED or POWERTOOLS_PARAMETERS_MAX_AGE to an empty string or to maybe and every invocation used to die with ValueError. They now emit a PowertoolsUserWarning and fall back to the default, all through one resolver, and an explicit argument always wins. Thank you @krishbuilds01, @AP24110011695 and @lechakrawarthy.

Idempotency

We fixed all nine items from the audit in #8603:

  • When your function returns something the serializer or json.dumps cannot handle, we still raise IdempotencyPersistenceLayerError, but we now clear the in-progress record first. The next call runs the function instead of hitting IdempotencyAlreadyInProgressError for an hour.
  • PydanticSerializer and DataclassSerializer resolve return annotations under from __future__ import annotations. A container such as list[Model] raises IdempotencyModelTypeError instead of a raw TypeError.
  • With a DynamoDB composite key, DataRecord.idempotency_key carries the operation's key, so the local cache and your response hooks see the right value instead of the static partition key.
  • Payloads with set, frozenset or bytes hash deterministically.
  • Redis: we retry when the record vanishes between SET NX and GET instead of failing the invocation, and the orphan lock TTL follows expires_after_seconds instead of always being ten seconds.
  • Reuse one persistence layer with a different IdempotencyConfig and we warn you; the first config still wins.
  • The IdempotencyConfig docstring now says local_cache_max_items defaults to 256, like the code does.

Thank you @sumedh-aerram, @Neeraj111010, @coughlmao, @CJstate, @lechakrawarthy and @soujasK.

Metrics

  • add_metric now skips a NaN or infinite value with a PowertoolsUserWarning. Before, json.dumps wrote it bare into the EMF line and CloudWatch dropped every metric in that flush without a word. single_metric also stops printing a null line when the with body raises. Thank you @anishmehta24.
  • POWERTOOLS_METRICS_DISABLED now silences single_metric, the cold start metric, the auto-flush at 100 metrics and the Datadog provider, not only flush_metrics(). POWERTOOLS_DEV=true disables metrics on every path too. Thank you @soujasK.

Logger

thread_safe_remove_keys now copies the thread-local dict before removing a key, so a remove inside an asyncio task or an asyncio.to_thread worker stays in that task instead of wiping the key for the caller and everyone else. Thank you @haeganm.

Parser, Event Handler and Data Classes

  • The Cognito trigger models accept the events Cognito really sends: CUSTOM_CHALLENGE in auth challenge sessions, answerCorrect as null, and every documented triggerSource for pre sign-up, post confirmation and the custom senders. We checked them against a live user pool.
  • The SES models accept a RequestResponse action and emails without To or Subject; the data class returns None for headers the email does not carry.
  • APIGatewayProxyEventModel keeps the Lambda authorizer's principalId, integrationLatency and context keys.
  • SQSRecord.queue_url builds the right DNS suffix for China, the ISO partitions and aws-eusc.
  • Path(alias=...) emits the alias in the OpenAPI schema when it matches the route template, a regression we introduced in 3.39.0.

Thank you @Manishram-ai, @dhairyakhandelwal880-dotcom and @anishmehta24 for the reports and the fixes.

Changes

  • docs(idempotency): fix local_cache_max_items default in IdempotencyConfig docstring (#8731) by @lechakrawarthy
  • fix(idempotency): treat empty or unparsable POWERTOOLS_IDEMPOTENCY_DISABLED as not disabled (#8734) by @lechakrawarthy
  • fix(metrics): respect POWERTOOLS_METRICS_DISABLED in every emission path (#8668) by @soujasK
  • fix(idempotency): compute the Redis orphan lock timeout from the configured expiry (#8779) by @CJstate
  • chore(ci): regenerate the agent skill references in the release instead of after every docs merge (#8811) by @leandrodamascena
  • fix(idempotency): retry Redis inconsistent state errors (#8736) by @coughlmao
  • fix(idempotency): add deterministic support for set, frozenset, and bytes types (#8741) by @Neeraj111010
  • fix(parameters): ignore malformed POWERTOOLS_PARAMETERS_MAX_AGE instead of crashing (#8757) by @AP24110011695
  • fix(logger): empty or unparsable POWERTOOLS_DEV no longer crashes Logger (#8783) by @krishbuilds01

🌟New features and non-breaking changes

πŸ“œ Documentation updates

  • feat(auth): add SigV4 and SigV4a request signing (#8820) by @leandrodamascena
  • feat(auth): graduate the Auth utility from auth_alpha to auth (#8818) by @leandrodamascena
  • fix(metrics): skip non-finite metric values with a warning (#8797) by @anishmehta24
  • fix(idempotency): warn when a persistence layer is reused with a different config (#8664) by @soujasK
  • fix(idempotency): clear the in progress record when the response can't be serialized (#8740) by @sumedh-aerram
  • fix(idempotency): resolve postponed return annotations for serializer classes (#8738) by @sumedh-aerram
  • fix(idempotency): use the record key for DynamoDB composite items (#8739) by @sumedh-aerram
  • chore(docs): regenerate the agent skill references after merge instead of in every PR (#8807) by @leandrodamascena

πŸ› Bug and hot fixes

πŸ”§ Maintenance

This release was made possible by the following contributors:

@anishmehta24, @AP24110011695, @CJstate, @coughlmao, @dhairyakhandelwal880-dotcom, @haeganm, @krishbuilds01, @leandrodamascena, @lechakrawarthy, @Manishram-ai, @Neeraj111010, @soujasK, @sumedh-aerram and @dependabot[bot]

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

NewReleases is sending notifications on new releases.