CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-aws-lambda-powertools

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

89

1.21x
Overview
Eval results
Files

task.mdevals/scenario-2/

Order Processing Lambda Handler

Build a Lambda function that processes incoming order events from an SQS queue. The function should validate and process each order, handle failures gracefully, and only report back messages that could not be successfully processed.

Requirements

Batch Processing

Your Lambda handler will receive multiple order messages in a single invocation. Each order must be processed independently, and partial failures must be reported so that only failed messages are returned to the queue for retry.

Order Validation

Each order message contains JSON data with the following fields:

  • order_id (string): Unique order identifier
  • customer_id (string): Customer identifier
  • items (array): List of items being ordered
  • total_amount (number): Total order amount

The function should:

  • Parse and validate each order message
  • Skip processing for orders where total_amount is less than 0 (treat as validation failure)
  • Skip processing for orders where items array is empty (treat as validation failure)

Processing Logic

For valid orders:

  1. Print a log message: "Processing order {order_id} for customer {customer_id}"
  2. Simulate processing by checking if order_id contains the string "fail" (case-insensitive)
    • If it contains "fail", raise an exception to simulate a processing error
    • Otherwise, consider the order successfully processed

Test Cases

  • When given a batch with one valid order (order_id="ORD-001", customer_id="CUST-123", items=["item1"], total_amount=99.99), it processes successfully and returns no failures @test
  • When given a batch with one order containing negative total_amount, it skips the order and reports it as failed @test
  • When given a batch with one order containing empty items array, it skips the order and reports it as failed @test
  • When given a batch with mixed valid and invalid orders, it processes valid ones and reports only invalid ones as failed @test
  • When given a batch where an order_id contains "FAIL", it catches the exception and reports that order as failed @test

Implementation

@generates

API

def lambda_handler(event: dict, context) -> dict:
    """
    Lambda handler that processes SQS order messages with partial failure handling.

    Args:
        event: Lambda event containing SQS records
        context: Lambda context object

    Returns:
        dict with batchItemFailures for messages that failed processing
    """
    pass

def process_order(order_data: dict) -> None:
    """
    Process a single order after validation.

    Args:
        order_data: Parsed order data dictionary

    Raises:
        Exception: If order processing fails
    """
    pass

Dependencies { .dependencies }

aws-lambda-powertools { .dependency }

Provides utilities for AWS Lambda best practices including batch processing with partial failure handling.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/pypi-aws-lambda-powertools

tile.json