Search code examples
pythonaws-lambdamypy

mypy type stubs for AWS lambda function


Are there maintained mypy types published for AWS Lambda functions?

I'm talking here about defining a function to handle an HTTP request (from a lambda URL or API Gateway). I'm not talking about other AWS Lambda-related APIs such as are exposed for example by boto3. Here's a example of the kind of function I would like to have types for, taken from AWS's docs:

def lambda_handler(event, context):
    message = 'Hello {} {}!'.format(event['first_name'], event['last_name'])
    return {
        'message' : message
    }

I'd like to type functions like that one as much as possible - for example, the context parameter (please ignore the fact that this trivial example does not happen to use parameter context).

I'm aware that AWS schema registry provides "bindings" for Event Bridge events, but note this question is about invoking lambdas in response to HTTP requests, not Event Bridge events.

I'm also aware that for example, if I used FastAPI together with a library like Mangum, I'd get types. I'm restricting this question just to use of the "native" AWS Lambda API (see the example code above).

I'm also aware that I could define my own types, but here I'm asking about types maintained by AWS or a third party.


Solution

  • Check out Powertools for AWS Lambda.

    It includes typings for LambdaContext as well as dataclasses for event sources such as ALBEvent.

    from aws_lambda_powertools.utilities.data_classes import ALBEvent
    from aws_lambda_powertools.utilities.typing import LambdaContext
    
    
    @event_source(data_class=ALBEvent)
    def lambda_handler(lambda_event: ALBEvent, context: LambdaContext) -> dict[str, Any]:
        ...