or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/aws-lambda-powertools@3.19.x
tile.json

tessl/pypi-aws-lambda-powertools

tessl install tessl/pypi-aws-lambda-powertools@3.19.0

Comprehensive developer toolkit implementing serverless best practices for AWS Lambda functions in Python

Agent Success

Agent success rate when using this tile

89%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.22x

Baseline

Agent success rate without this tile

73%

task.mdevals/scenario-4/

E-Commerce Order Processing Metrics

Overview

Build a Lambda function that processes e-commerce order events and emits custom CloudWatch metrics with dimensional data and contextual metadata for analytics and monitoring.

Problem Statement

You are building an observability system for an e-commerce platform. The system needs to track order processing metrics with rich dimensional data (for aggregation and filtering) and metadata (for detailed investigation).

Your Lambda function will:

  1. Process incoming order events
  2. Emit metrics about order value, quantity, and processing status
  3. Add dimensional data for filtering by region, product category, and payment method
  4. Include metadata about customer segments, promotion codes, and warehouse locations for detailed analysis

Requirements

Input Event Structure

The Lambda function receives events with this structure:

{
  "order_id": "ORD-12345",
  "region": "us-west",
  "product_category": "Electronics",
  "payment_method": "credit_card",
  "order_value": 299.99,
  "quantity": 2,
  "customer_segment": "premium",
  "promotion_code": "SUMMER2024",
  "warehouse_location": "warehouse-seattle-01",
  "status": "completed"
}

Metric Requirements

Emit the following metrics with appropriate units:

  1. OrderValue - The monetary value of the order (use appropriate unit for currency)
  2. OrderQuantity - The number of items in the order (use count unit)

Dimensional Data Requirements

Add the following dimensions to ALL metrics for filtering and aggregation:

  • Region - The geographic region (from event)
  • ProductCategory - The product category (from event)
  • PaymentMethod - The payment method used (from event)

These dimensions should allow CloudWatch to aggregate metrics by any combination of these attributes.

Metadata Requirements

Add the following as metadata (NOT dimensions) for detailed investigation:

  • CustomerSegment - The customer tier/segment
  • PromotionCode - Any applied promotion code
  • WarehouseLocation - The fulfillment warehouse

Metadata should be searchable in CloudWatch Logs Insights but should not count toward dimension limits.

Implementation Details

  • The function should handle the Lambda event/context pattern
  • Metrics should be emitted asynchronously using the CloudWatch Embedded Metric Format
  • All metrics should be grouped under a namespace: EcommerceMetrics
  • Use a service identifier: OrderProcessing
  • Ensure proper metric publishing at function completion

Test Cases { .tests }

Test 1: Single Order Processing { .test }

Input Event:

{
  "order_id": "ORD-001",
  "region": "us-east",
  "product_category": "Books",
  "payment_method": "paypal",
  "order_value": 45.50,
  "quantity": 3,
  "customer_segment": "standard",
  "promotion_code": "BOOKWORM",
  "warehouse_location": "warehouse-nyc-02",
  "status": "completed"
}

Expected Behavior:

  • OrderValue metric emitted with value 45.50
  • OrderQuantity metric emitted with value 3
  • Dimensions: Region=us-east, ProductCategory=Books, PaymentMethod=paypal
  • Metadata includes: CustomerSegment=standard, PromotionCode=BOOKWORM, WarehouseLocation=warehouse-nyc-02

Test 2: Premium Order Processing { .test }

Input Event:

{
  "order_id": "ORD-002",
  "region": "eu-west",
  "product_category": "Electronics",
  "payment_method": "credit_card",
  "order_value": 1299.99,
  "quantity": 1,
  "customer_segment": "premium",
  "promotion_code": "VIP10",
  "warehouse_location": "warehouse-london-01",
  "status": "completed"
}

Expected Behavior:

  • OrderValue metric emitted with value 1299.99
  • OrderQuantity metric emitted with value 1
  • Dimensions: Region=eu-west, ProductCategory=Electronics, PaymentMethod=credit_card
  • Metadata includes: CustomerSegment=premium, PromotionCode=VIP10, WarehouseLocation=warehouse-london-01

File Structure

Create the following files:

src/
├── handler.py          # Lambda handler function
└── handler_test.py     # Test file with test cases

Dependencies { .dependencies }

aws-lambda-powertools { .dependency }

Provides observability utilities for AWS Lambda functions including structured logging, metrics, and tracing.