Package: lambda-decorators
A collection of useful decorators for making AWS Lambda handlerslambda_decoratorsis a collection of useful decorators for writing Python
handlers forAWS Lambda. They allow you to
avoid boiler plate for common things such as CORS headers, JSON serialization,
etc.Quick example# handler.pyfromlambda_decoratorsimportjson_http_resp,load_json_body@json_http_resp@load_json_bodydefhandler(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"}InstallIf you are usingthe serverless frameworkI recommend usingserverless-python-requirementsslsplugininstall-nserverless-python-requirementsecholambda-decorators>>requirements.txtOr if using some other deployment method to AWS Lambda you can just download
the entire module because it’s only one file.curl-Ohttps://raw.githubusercontent.com/dschep/lambda-decorators/master/lambda_decorators.pyIncluded Decorators:lambda_decoratorsincludes the following decorators to avoid boilerplate
for common usecases when using AWS Lambda with Python.async_handler- support for async handlerscors_headers- automatic injection of CORS headersdump_json_body- auto-serialization of http body to JSONload_json_body- auto-deserialize of http body from JSONjson_http_resp- automatic serialization of python object to HTTP JSON responsejson_schema_validator- use JSONSchema to validate request&response payloadsload_urlencoded_body- auto-deserialize of http body from a querystring encoded bodyno_retry_on_failure- detect and stop retry attempts for scheduled lambdasssm_parameter_store- fetch parameters from the AWS SSM Parameter Storesecret_manager- fetch secrets from the AWS Secrets ManagerSee each individual decorators for specific usage details and theexamplefor some more use cases. This library is also meant to serve as an example for how to write
decorators for use as lambda middleware. See therecipespage for some more niche examples of
using decorators as middleware for lambda.Writing your ownlambda_decoratorsincludes utilities to make building your own decorators
easier. Thebefore,after, andon_exceptiondecorators
can be applied to your own functions to turn them into decorators for your
handlers. For example:importloggingfromlambda_decoratorsimportbefore@beforedeflog_event(event,context):logging.debug(event)returnevent,context@log_eventdefhandler(event,context):return{}And if you want to make a decorator that provides two or more of
before/after/on_exception functionality, you can useLambdaDecorator:importloggingfromlambda_decoratorsimportLambdaDecoratorclasslog_everything(LambdaDecorator):defbefore(event,context):logging.debug(event,context)returnevent,contextdefafter(retval):logging.debug(retval)returnretvaldefon_exception(exception):logging.debug(exception)return{'statusCode':500}@log_everythingdefhandler(event,context):return{}WhyInitially, I was inspired bymiddywhich
I like using in JavaScript. 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,decoratorspretty 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.Full API Documentation