• Stars
    star
    247
  • Rank 164,117 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🐍λ✨ - A collection of useful decorators for making AWS Lambda handlers

🐍λ✨ - 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

More Repositories

1

ntfy

πŸ–₯οΈπŸ“±πŸ”” A utility for sending notifications, on demand and when commands finish.
Python
4,781
star
2

proven

πŸ”‘βœ… An alternative to Twitter's verified accounts powered by Keybase.
JavaScript
514
star
3

imgur-album-downloader

πŸ–ΌοΈβ¬‡οΈ A Pure client-side webapp to download entire or parts of Imgur albums.
HTML
170
star
4

geoip-lambda-layer

An example Lambda Layer containing MaxMind's free GeoIP DBs
Dockerfile
44
star
5

install-poetry-action

A Github action to install poetry
TypeScript
31
star
6

HELPeR

ABANDONED - HELPeR is an open source, self hosted, easy to hack clone of IFTTT
Python
25
star
7

sqlite-lambda-layer

A project providing a Lambda Layer that provides SQLite support in Python3.6 Lambdas
Dockerfile
25
star
8

install-pipenv-action

A Github action to install pipenv
TypeScript
24
star
9

django-xor-formfields

Mutually Exclusive Fields&Widgets for Django.
Python
18
star
10

github-oneclick-commit

:octocat::shipit: A silly browser extension inspired by @HackerNewsOnion
JavaScript
15
star
11

ntfy-webpush

β˜οΈπŸ”” webpush notification support for ntfy
JavaScript
14
star
12

serverless-cgi

βš‘οΈπŸ—‘ 2017 meets 1997
Python
11
star
13

license-checker

A Single-page html/js GitHub license checker
JavaScript
10
star
14

dc-bike-finder

πŸš²πŸ—ΊοΈ A webapp for finding bikeshares in DC
Python
9
star
15

owntracks-serverless

βš‘πŸ—ΊοΈ An AWS Lambda Backend for OwnTracks
JavaScript
8
star
16

bikehero

πŸš²πŸ”§ A webapp for crowd-sourced Fixit-stands & bike pumps
JavaScript
8
star
17

serverless-pushover

βš‘πŸ“² Forward notifications from the Serverless Dashboard to Pushover
Python
6
star
18

django-photomap

A simple Django app to provide a map with user submitted phots
Python
6
star
19

hows-my-driving-dc

twitter bot for looking up traffic & parking violations in dc
Python
6
star
20

box

Ansible scripts provision a computer to my tastes
Vim Script
5
star
21

filter-event-action

A Github Action to filter by event contents
Dockerfile
5
star
22

geojson-viewer

A simple GeoJSON viewer that loads data via CORS
HTML
5
star
23

bikehero-labs

πŸš²βš—οΈ small bikey projects
HTML
4
star
24

sls-py-tmpl

A better Python template for serverless
Python
4
star
25

serverless-graphile

βš‘πŸ˜πŸ•ΈοΈ Serverless project for deploying a PostGraphile service
JavaScript
3
star
26

fountainhead-status

http://fountainhead-stat.us/
Python
3
star
27

metal-wapo

πŸ€˜πŸ“° Replace The Washington Post's "Democracy Dies in Darkness" masthead with random metal albums
JavaScript
3
star
28

jQuery-Bookmarklet

A simple plugin to make bookmarklets easy to install
JavaScript
2
star
29

Twitter-user_timeline.rss-proxy

Simple proxy that mimics the API v1 usage for getting user's timelines as an RSS feed
Python
2
star
30

GistMarklets

{}πŸ”– Easy to use installation page for bookmarklets hosted as gists.
CSS
2
star
31

nuvola-app-synology-audio-station

Synology Audio Station for Nuvola Player 3
JavaScript
2
star
32

twitter2rss

JavaScript
2
star
33

rdsdataapi

ABANDONED - DB-API 2.0 driver & SqlAlchemy dialect for the AWS RDS Data API
Python
2
star
34

OpenReadability

A cleaned up fork of Arc90's Readability
JavaScript
2
star
35

executive-orders-notebook

A Jupyter Notebook to play with how many Executive Orders Presidents have signed
Jupyter Notebook
1
star
36

grease_monkey_scripts

GreaseMonkey expirements
JavaScript
1
star
37

mapp

a material-ui + leaflet PWA quickstart
JavaScript
1
star
38

ansible-docker

An un-fancy ansible module to install docker on Ubuntu
1
star
39

serverless-at

A serverless implementation of the UNIX at command
JavaScript
1
star
40

jot

πŸ“ A stupid simple launcher for jotting down notes without having to remembering where they are
Python
1
star
41

PyCoCoRaHS

a Python package providing a CLI utility and an API for uploading observations to CoCoRaHS
Python
1
star