AWS Lambda Typing

2.20.0 · active · verified Sat Apr 11

A Python package providing comprehensive type hints for AWS Lambda event, context, and response objects. It enhances developer experience with IDE autocomplete and static type checking, without runtime impact. The library is actively maintained with frequent updates to support new Lambda event structures.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic AWS Lambda handler using type hints for an API Gateway HTTP API (v2) event and context object, and specifies the expected response type. It illustrates how to access common event fields and context attributes with IDE assistance.

import os
from aws_lambda_typing.context import Context
from aws_lambda_typing.events.api_gateway import APIGatewayProxyEventV2
from aws_lambda_typing.responses.api_gateway import APIGatewayProxyResponseV2

def lambda_handler(
    event: APIGatewayProxyEventV2,
    context: Context
) -> APIGatewayProxyResponseV2:
    path = event.get('requestContext', {}).get('http', {}).get('path', '/')
    method = event.get('requestContext', {}).get('http', {}).get('method', 'GET')
    query_params = event.get('queryStringParameters', {})

    print(f"Received {method} request for {path} with query params: {query_params}")

    # Example of accessing context object
    print(f"Lambda function name: {context.function_name}")

    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'application/json'},
        'body': '{"message": "Hello from typed Lambda!"}'
    }

view raw JSON →