Skip to main content

🐍λ✨ - lambda_decorators

Version Docs Build SayThanks

A collection of useful decorators for making AWS Lambda handlers

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.

See each individual decorators for specific usage details and the example for 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 the recipes page for some more niche examples of using decorators as middleware for lambda.

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 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, 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.


Full API Documentation

Metadata

Release files for lambda-decorators 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lambda-decorators 0.6.0
File Size Uploaded
lambda-decorators-0.6.0.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lambda-decorators 0.6.0
File Interpreter ABI Platform
lambda_decorators-0.6.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 19.2 kB

Release files / lambda-decorators-0.6.0.tar.gz

Download URL lambda-decorators-0.6.0.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3043cfbc7cf5633bc5345f7827291dca6090864ff792f609dc2bce75c2cfb7f0
BLAKE2b-256 checksum
How to use checksums
8d0edfad34fbd523758cdaa31130644df87bd1e9ea64d8bafd950d6c6461312c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.8

Release files / lambda_decorators-0.6.0-py2.py3-none-any.whl

Download URL lambda_decorators-0.6.0-py2.py3-none-any.whl
Size 10.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6dae10db0b958e6cb41e162e160254821753cd2c654e3167d89639e8c6217e8b
BLAKE2b-256 checksum
How to use checksums
0d3a7e4a727e2fa86ccb0202322da6062e01259d03ffd73575515d8d44ccfe68
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.8
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page