AWS Lambda Typing
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
- gotcha Older versions of `aws-lambda-typing` (prior to 2.17.1) might have incorrectly marked all fields in `APIGatewayProxyResponseV1` as required due to default `TypedDict` behavior, leading to static analysis errors if optional fields were omitted. Similarly, API Gateway event fields were made `Optional` in v2.16.4.
- gotcha The structure of AWS Lambda event objects frequently evolves. Using an outdated version of `aws-lambda-typing` may lead to missing or incorrect type definitions for newer event structures, causing type checker warnings or runtime `KeyError` if types are used for validation.
- gotcha While `aws-lambda-typing` provides robust types for Lambda events and context, it does not include types for `boto3` service responses. Mixing `boto3` response structures directly with `aws-lambda-typing` event types can lead to type mismatches.
Install
-
pip install aws-lambda-typing
Imports
- Context
from aws_lambda_typing.context import Context
- APIGatewayProxyEventV2
from aws_lambda_typing.events.api_gateway import APIGatewayProxyEventV2
- SQSEvent
from aws_lambda_typing.events.sqs import SQSEvent
- APIGatewayProxyResponseV2
from aws_lambda_typing.responses.api_gateway import APIGatewayProxyResponseV2
- APIGatewayProxyEventV1
from aws_lambda_typing.events.api_gateway import APIGatewayProxyEventV1
Quickstart
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!"}'
}