Comprehensive developer toolkit implementing serverless best practices for AWS Lambda functions in Python
89
Build a Lambda function that transforms log records from Kinesis Data Firehose by filtering, enriching, and formatting them before delivery to S3.
Your Lambda function should:
Filter logs by severity: Only process records with log level "ERROR" or "WARN". Drop all other records.
Parse and validate: Parse each record as JSON. If parsing fails, mark the record as processing failed.
Enrich records: Add a processed_at timestamp field (ISO 8601 format) to each valid record.
Format output: Return transformed records as JSON strings, properly base64-encoded.
Handle status codes: Return appropriate result codes:
Each incoming record contains:
recordId: Unique identifier for the recorddata: Base64-encoded JSON string with fields:
level: Log level (DEBUG, INFO, WARN, ERROR)message: Log message texttimestamp: Original log timestampReturn a response with transformed records containing:
recordId: Original record IDresult: Status code (Ok, Dropped, or ProcessingFailed)data: Base64-encoded transformed JSON (for Ok status)@generates
def lambda_handler(event: dict, context: Any) -> dict:
"""
Transform Kinesis Firehose records.
Args:
event: Firehose transformation event
context: Lambda context
Returns:
Firehose transformation response with records
"""Provides event handling and data transformation utilities for AWS Lambda.
Install with Tessl CLI
npx tessl i tessl/pypi-aws-lambda-powertoolsdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10