AWS Lambda Powertools (Python)

3.27.0 · active · verified Mon Apr 06

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.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the `Logger` utility, automatically enriching logs with Lambda context and logging the incoming event. It's designed to run in an AWS Lambda environment.

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."),
    }

view raw JSON →