A collection of useful decorators for making AWS Lambda handlers
Project description
lambda-handlers
An opinionated Python package that facilitates specifying AWS Lambda handlers.
It includes input validation, error handling and response formatting.
Getting started
To use lambda-handlers
you must first install it:
pip install lambda-handlers
If you are going to use validation, you should choose between Marshmallow or jsonschema.
To install with one of these:
pip install 'lambda-handlers[marshmallow]'
or
pip install 'lambda-handlers[jsonschema]'
Quickstart
By default the http_handler
decorator makes sure of parsing the request body
as JSON, and also formats the response as JSON with:
- an adequate statusCode,
- CORS headers, and
- the handler return value in the body.
from lambda_handler import http_handler
@http_handler()
def handler(event, context):
return event['body']
Examples
Skipping the CORS headers default and configuring it.
from lambda_handler import http_handler
from lambda_handlers.response import cors
@http_handler(
cors=cors(origin='localhost', credentials=False),
)
def handler(event, context):
return event['body']
Using jsonschema to validate a the input of a User model.
from typing import Dict, Any
from lambda_handler import validators, http_handler
user_schema: Dict[str, Any] = {
'type': 'object',
'properties': {
'user_id': {'type': 'number'},
},
}
@http_handler(
validation=validators.jsonschema(body=user_schema),
)
def handler(event, context):
user = event['body']
return user
Using Marshmallow to validate a User model in the input and in the response body.
from lambda_handler import validators, http_handler
from marshmallow import Schema, fields
class UserSchema(Schema):
user_id = fields.Integer(required=True)
class ResponseSchema(Schema):
body = fields.Nested(UserSchema, required=True)
headers = fields.Dict(required=True)
statusCode = fields.Integer(required=True)
@http_handler(
validation=validators.marshmallow(
body=UserSchema(),
response=ResponseSchema(),
),
)
def handler(event, context):
user = event['body']
return user
Development
This project uses pipenv to manage its dependencies and Python environment. You can install it by:
pip install --user pipenv
We recommend using a Python virtual environment for each separate project you do. For that, we suggest using pyenv.
Installation
For production, after you clone this repository, you can install this project plus dependencies with:
cd <clone_dest>
make install
Development
For development you should also install the development dependencies, so run instead:
cd <clone_dest>
make install-dev
This will install all dependencies and this project in development mode.
Testing
We use tox to run the code checkers.
You can run the tests by running tox
in the top-level of the project.
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
File details
Details for the file lambda-handlers-1.0.5.tar.gz
.
File metadata
- Download URL: lambda-handlers-1.0.5.tar.gz
- Upload date:
- Size: 18.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d177a90c3df7c13dbc7f905dd94880d8eed17eddf055cbe0a01deb051f8eebfb |
|
MD5 | 1e15fe4205e6d3f8b4e49749108a9a3f |
|
BLAKE2b-256 | e47e345fd13a05d4fc255bc392566e47ad53455184e0cd307da042101285a7cf |
File details
Details for the file lambda_handlers-1.0.5-py3-none-any.whl
.
File metadata
- Download URL: lambda_handlers-1.0.5-py3-none-any.whl
- Upload date:
- Size: 24.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e797ae1240a37a64da06ad89afc0124fafdbbaf8a40dbc4e8398575acd6f0d26 |
|
MD5 | 86a29267785867933a9962a81fa35583 |
|
BLAKE2b-256 | bcb8d8626624e648ccfdbc3dae85d2ae15783effc070695e4dc6a5650f9b090c |