AWS Lambda Powertools (Python)
Powertools for AWS Lambda (Python) is a developer toolkit designed to implement Serverless best practices and increase developer velocity. It offers utilities for tracing, logging, metrics, event handling, data parsing, and idempotency. The library is currently at version 3.27.0 and maintains an active development cycle with frequent releases, often shipping new features and bug fixes on a monthly basis.
Common errors
-
ModuleNotFoundError: No module named 'aws_lambda_powertools'
cause The 'aws_lambda_powertools' module is not included in the Lambda deployment package.fixEnsure 'aws-lambda-powertools' is installed and included in your deployment package. If using AWS SAM, add it to your 'requirements.txt' and include it in the build process. -
ImportError: /lib64/libc.so.6: version GLIBC_2.XX not found
cause The Lambda function is using a compiled extension that requires a different version of the GNU C Library (GLIBC) than what is available in the Lambda runtime.fixUse Docker with the appropriate Lambda base image to build your dependencies, ensuring compatibility with the Lambda runtime environment. -
RequestEntityTooLargeException
cause The Lambda deployment package exceeds the maximum allowed size.fixOptimize your deployment package by using Lambda Layers for large dependencies, removing unnecessary files, and considering the use of container images for very large packages. -
ImportError: cannot import name 'Logger' from 'aws_lambda_powertools'
cause The 'Logger' class is not found in the 'aws_lambda_powertools' module, possibly due to an incorrect import statement or version mismatch.fixEnsure you are using the correct import statement: 'from aws_lambda_powertools import Logger'. Also, verify that you have the latest version of 'aws-lambda-powertools' installed. -
AttributeError: module 'aws_lambda_powertools' has no attribute 'Tracer'
cause The 'Tracer' attribute is not found in the 'aws_lambda_powertools' module, likely due to an incorrect import statement or version mismatch.fixEnsure you are using the correct import statement: 'from aws_lambda_powertools import Tracer'. Also, verify that you have the latest version of 'aws-lambda-powertools' installed.
Warnings
- breaking AWS Lambda Powertools v3.24.0 and later have dropped support for Python 3.9. Attempting to use these versions with a Python 3.9 runtime will result in errors.
- gotcha Versions prior to v3.19.0 of AWS Lambda Powertools had critical bugs affecting Data validation and Middleware within the Event Handler utility, potentially leading to incorrect processing or validation errors.
- gotcha A regression was introduced in v3.22.0 affecting the Event Handler utility when using nested metadata annotations (e.g., `annotated_types` or `Interval`).
- gotcha AWS Lambda Powertools v3 is compatible with Pydantic V2. Users migrating from Pydantic V1 might need to update their model syntax (e.g., `model_validator` instead of `validator`) when using models with Event Handler or Parser utilities.
Install
-
pip install aws-lambda-powertools
Imports
- Logger
from aws_lambda_powertools import Logger
- Tracer
from aws_lambda_powertools import Tracer
- Metrics
from aws_lambda_powertools import Metrics
- App
from aws_lambda_powertools import App
from aws_lambda_powertools.event_handler import App
- Parser
from aws_lambda_powertools.utilities.parser import Parser
- Idempotency
from aws_lambda_powertools.utilities.idempotency import IdempotencyConfig, idempotent
Quickstart
import json
from aws_lambda_powertools import Logger
logger = Logger(service="payment_processing")
@logger.inject_lambda_context(log_event=True)
def handler(event, context):
# Accessing event data via a common pattern
if event and 'body' in event:
try:
body = json.loads(event['body'])
transaction_id = body.get('transaction_id', 'N/A')
logger.info(f"Processing transaction: {transaction_id}")
except json.JSONDecodeError:
logger.error("Invalid JSON in event body")
return {"statusCode": 400, "body": json.dumps({"message": "Invalid JSON"})}
else:
logger.info("No event body provided.")
logger.info("Hello from Lambda, using Powertools Logger!")
return {
"statusCode": 200,
"body": json.dumps("Successfully processed request."),
}