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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c52c84846d499b26f74b2f9e06869e803a16019255d661fc937ef86541c7697c
|
|
| MD5 |
51b422e5ae50d6483c92a3bb22f7f83a
|
|
| BLAKE2b-256 |
bcc2672398cd55eb887c756b8df32b23df51def2a3da2f97a55644c292d55554
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67c7c236216efdb5cbf33be4398f84a9a2a2d3b7c7967bffc1b61031245e529d
|
|
| MD5 |
48c0c946da6a5cd0d7ebc2b9c2014e39
|
|
| BLAKE2b-256 |
11ae67237fb35fa40ae7734eb4d6e067847677933334ed33d8fbf6a23040c7ab
|