Skip to main content

A Python package for routing and validating AWS events inside a Lambda function

Project description

Lambda Handler

This project defines a Python class, LambdaHandler, and associated Pydantic-derived event classes, for handling API Gateway events from a range of sources, in particular:

  • Direct Invocation
  • EventBridge
  • SQS queues
  • SNS topics

When not using the optional FastAPI support (see below), the package's only dependency is pydantic.

Use

from lambda_handler import (
    LambdaHandler,
    EventBridgeEvent,
    SnsEvent,
    LambdaResponse,
)

handler = LambdaHandler()

@handler.sns(topic_name="MyTopic")
def handle_mytopic(event: SnsEvent) -> LambdaResponse:
    body = frobincate()
    return LambdaResponse(status_code=200, body=body)

@handler.event_bridge(resource_name="MyResource")
def handle_myresource(event: EventBridgeEvent) -> LambdaResponse:
    body = fizzbuzz()
    return LambdaResponse(status_code=200, body=body)

The handler looks after both the event parsing (so your functions should always accept an event of some *Event type), and its response as a properly-formatted dictionary.

Combining with FastAPI

A notable omission from the events that are handled by LambdaHandler directly are HTTP requests. These can be handled by an instance of FastAPI, as follows:

from fastapi import FastAPI
from lambda_handler import LambdaHandler

app = FastAPI(title="My HTTP handler")

@app.get("/")
def index():
    return "Hello, World!"

handler = LambdaHandler(fastapi_app=app)

The handler will then take care of everything on your behalf. If you'd prefer, you can set fastapi_app later instead, and the handler will take care of that, too.

from fastapi import FastAPI
from lambda_handler import LambdaHandler, SnsEvent, LambdaResponse

handler = LambdaHandler()

@handler.sns(topic_name="MyTopic")
def handle_mytopic(event: SnsEvent) -> LambdaResponse:
    body = frobincate()
    return LambdaResponse(status_code=200, body=body)


app = FastAPI(title="My HTTP handler")

@app.get("/")
def index():
    return "Hello, World!"

handler.fastapi_app = app

FastAPI support requires the package to be installed with optional extras: pip install "lambda-handler[fastapi]", and is built on top of the existing Mangum package.

Model Validation

The *Event models lambda-handler defines use pydantic for parsing and validation, and these models are generic. This means that you can pass a type argument to the class when defining your function, and it will correctly parse the content of the event (see below) to that type. If this is confusing, it's easier to see it in action:

from lambda_handler import LambdaHandler, SnsEvent, LambdaResponse
from pydantic import BaseModel

handler = LambdaHandler()

class MyModel(BaseModel):
    thing: str

@handler.sns(topic_name=topic_name)
def test_func(event: SnsEvent[MyModel]) -> LambdaResponse:
    assert isinstance(event.records[0].sns.message, MyModel)
    return LambdaResponse(status_code="200")

Here, we have parametrised SnsEvent with MyModel in the signature of test_func, meaning that the message attribute is parsed to a MyModel instance in the process.

Parametrised Event Attributes

The following attributes are those which are parsed to a Pydantic model for each event type:

Event Type Parsed Attribute
DirectInvocationEvent event.direct_invocation.body
EventBridgeEvent event.detail
SnsEvent event.records[i].sns.message
SqsEvent event.records[i].body

Dealing with Raw Data

If you don't want to deal with parsed event objects, you can include the raw=True parameter to any of the wrapping methods of LambdaHandler and write a function that accepts and returns a Dict[str, Any] instead. Note that, in this case, the event object will still be parsed by the AwsEvent subclasses for identification, but the event object will be passed as-is in dictionary format to the function.

from fastapi import FastAPI
from lambda_handler import LambdaHandler, SnsEvent, LambdaResponse

from typing import Any, Dict

handler = LambdaHandler()

@handler.sns(topic_name="MyTopic")
def handle_mytopic(event: SnsEvent) -> LambdaResponse:
    body = frobincate()
    return LambdaResponse(status_code=200, body=body)

@handler.sns(topic_name="MyOtherTopic". raw=True)
def handle_mytopic(event: Dict[str, Any]) -> Dict[str, Any]:
    body = frobincate()
    return {"statusCode": "200"}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lambda_handler-2.0.0.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

lambda_handler-2.0.0-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file lambda_handler-2.0.0.tar.gz.

File metadata

  • Download URL: lambda_handler-2.0.0.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Darwin/22.5.0

File hashes

Hashes for lambda_handler-2.0.0.tar.gz
Algorithm Hash digest
SHA256 c52c84846d499b26f74b2f9e06869e803a16019255d661fc937ef86541c7697c
MD5 51b422e5ae50d6483c92a3bb22f7f83a
BLAKE2b-256 bcc2672398cd55eb887c756b8df32b23df51def2a3da2f97a55644c292d55554

See more details on using hashes here.

File details

Details for the file lambda_handler-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: lambda_handler-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 24.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Darwin/22.5.0

File hashes

Hashes for lambda_handler-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67c7c236216efdb5cbf33be4398f84a9a2a2d3b7c7967bffc1b61031245e529d
MD5 48c0c946da6a5cd0d7ebc2b9c2014e39
BLAKE2b-256 11ae67237fb35fa40ae7734eb4d6e067847677933334ed33d8fbf6a23040c7ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page