A collection of useful decorators for making AWS Lambda handlers
Project description
A collection of useful decorators for making AWS Lambda handlers
NOTE: this is in very early stages of development.
lambda_decorators is a collection of useful decorators for writing Python handlers for AWS Lambda. They allow you to avoid boiler plate for common things such as CORS headers, JSON serialization, etc.
Quick example
# handler.py
from lambda_decorators import json_http_resp, load_json_body
@json_http_resp
@load_json_body
def handler(event, context):
return {'hello': event['body']['name']}
When deployed to Lambda behind API Gateway and cURL’d:
$ curl -d '{"name": "world"}' https://example.execute-api.us-east-1.amazonaws.com/dev/hello
{"hello": "world"}
Install
If you are using the serverless framework I recommend using serverless-python-requirements
sls plugin install -n serverless-python-requirements
echo lambda-decorators >> requirements.txt
Or if using some other deployment method to AWS Lambda you can just download the entire module because it’s only one file.
curl -O https://raw.githubusercontent.com/dschep/lambda-decorators/master/lambda_decorators.py
Included Decorators:
lambda_decorators includes the following decorators to avoid boilerplate for common usecases when using AWS Lambda with Python.
async_handler - support for async handlers
cors_headers - automatic injection of CORS headers
dump_json_body - auto-serialization of http body to JSON
load_json_body - auto-deserialize of http body from JSON
json_http_resp - automatic serialization of python object to HTTP JSON response
json_schema_validator - use JSONSchema to validate request&response payloads
load_urlencoded_body - auto-deserialize of http body from a querystring encoded body
no_retry_on_failure - detect and stop retry attempts for scheduled lambdas
See each individual decorators for specific usage details and the example for some more use cases.
Writing your own
lambda_decorators includes utilities to make building your own decorators easier. The before, after, and on_exception decorators can be applied to your own functions to turn them into decorators for your handlers. For example:
import logging
from lambda_decorators import before
@before
def log_event(event, context):
logging.debug(event)
return event, context
@log_event
def handler(event, context):
return {}
And if you want to make a decorator that provides two or more of before/after/on_exception functionality, you can use LambdaDecorator:
import logging
from lambda_decorators import LambdaDecorator
class log_everything(LambdaDecorator):
def before(event, context):
logging.debug(event, context)
return event, context
def after(retval):
logging.debug(retval)
return retval
def on_exception(exception):
logging.debug(exception)
return {'statusCode': 500}
@log_everything
def handler(event, context):
return {}
Why
Initially, I was inspired by middy which I’ve tried out in JavaScript and was happy with it. So naturally, I thought I’d like to have something similar in Python too. But then as I thought about it more, it seemed that when thinking of functions as the compute unit, when using python, decorators pretty much are middleware! So instead of building a middleware engine and a few middlewares, I just built a few useful decorators and utilities to build them.
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
Hashes for lambda_decorators-0.1b2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49096c2ea756980318eb429063fc037c81d5d7fc607dc1bc7d1d37179c999521 |
|
MD5 | d50c83caec95c33bc14523c1a74c003f |
|
BLAKE2b-256 | d32862f8bec5dc9651281849296ca9b86e8ceb441974accb8fa0fd00da0dc483 |