Skip to main content

A collection of useful decorators for making AWS Lambda handlers

Project description

Version Docs Build

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 cors_headers, json_http_resp, load_json_body

@cors_headers
@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

pip install lambda-decorators

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.

Included Decorators:

See each individual decorators for specific usage details and the example for some more use cases.


Full API Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lambda-decorators-0.1b0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lambda_decorators-0.1b0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file lambda-decorators-0.1b0.tar.gz.

File metadata

File hashes

Hashes for lambda-decorators-0.1b0.tar.gz
Algorithm Hash digest
SHA256 51ad3cfea14b6dc667b1f07fbd504160ff0dbac51b2920f066dd981ffee429c3
MD5 92b88be13ea81f31023c1f047d09c0a8
BLAKE2b-256 bd8221ca1bf05047b8c3d2b45d092cbff3465fed960759ef870e2057c511cb66

See more details on using hashes here.

File details

Details for the file lambda_decorators-0.1b0-py3-none-any.whl.

File metadata

File hashes

Hashes for lambda_decorators-0.1b0-py3-none-any.whl
Algorithm Hash digest
SHA256 28732a6d011f0495d71319341b068c0b3e3a6faa455024cc134575868729471d
MD5 e63ed06cb4960644fa17c3b6ff53c110
BLAKE2b-256 e5923d06659fbd454e47399e0cb49e58381baf539c48545623bfd3892875e5c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page