Skip to main content

A collection of useful decorators for making AWS Lambda handlers

Project description

lambda-handlers

An opinionated Python package that facilitates specifying AWS Lambda handlers.

It includes input validation, error handling and response formatting.

Contents

Getting started

Install lambda-handlers with:

pip install lambda-handlers

If you are going to use validation, you should choose between Marshmallow or jsonschema.

To install with one of these:

pip install 'lambda-handlers[marshmallow]'

or

pip install 'lambda-handlers[jsonschema]'

Quickstart

By default the http_handler decorator makes sure of parsing the request body as JSON, and also formats the response as JSON with:

  • an adequate statusCode,
  • CORS headers, and
  • the handler return value in the body.
from lambda_handler import http_handler

@http_handler()
def handler(event, context):
    return event['body']

Examples

Skipping the CORS headers default and configuring it.

from lambda_handler import http_handler
from lambda_handlers.response import cors

@http_handler(
    cors=cors(origin='localhost', credentials=False),
)
def handler(event, context):
    return event['body']

Using jsonschema to validate a the input of a User model.

from typing import Dict, Any

from lambda_handler import validators, http_handler

user_schema: Dict[str, Any] = {
    'type': 'object',
    'properties': {
        'user_id': {'type': 'number'},
    },
}


@http_handler(
    validator=validators.jsonschema(body=user_schema),
)
def handler(event, context):
    user = event['body']
    return user

Using Marshmallow to validate a User model in the input and in the response body.

from lambda_handler import validators, http_handler
from marshmallow import Schema, fields


class UserSchema(Schema):
    user_id = fields.Integer(required=True)


class ResponseSchema(Schema):
    body = fields.Nested(UserSchema, required=True)
    headers = fields.Dict(required=True)
    statusCode = fields.Integer(required=True)


@http_handler(
    validator=validators.marshmallow(
        body=UserSchema(),
        response=ResponseSchema(),
    ),
)
def handler(event, context):
    user = event['body']
    return user

Using the source code

This project uses pipenv to manage its dependencies and Python environment. You can install it by:

pip install --user pipenv

We recommend using a Python virtual environment for each separate project you do. For that, we suggest using pyenv.

Installation

For production, after you clone this repository, you can install this project plus dependencies with:

cd <clone_dest>
make install

Development

For development you should also install the development dependencies, so run instead:

cd <clone_dest>
make install-dev

This will install all dependencies and this project in development mode.

Testing

We use tox to run the code checkers.

You can run the tests by running tox in the top-level of the project.

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-handlers-1.1.1.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

lambda_handlers-1.1.1-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file lambda-handlers-1.1.1.tar.gz.

File metadata

  • Download URL: lambda-handlers-1.1.1.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for lambda-handlers-1.1.1.tar.gz
Algorithm Hash digest
SHA256 2794b0f51cee6db43d45d358dd78974e09d0c0e76cff36d039690d67f1a8b696
MD5 1bd1762755dbc03b72ad172e82af74ad
BLAKE2b-256 2144edecde0245ce9a1771727f765941fb43595047c4dbe49fe8fd5af19fee2e

See more details on using hashes here.

File details

Details for the file lambda_handlers-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: lambda_handlers-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for lambda_handlers-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c4244463d689e28355149459e8d4478de3f4cc564d78cb83bc1d50cb9da068c
MD5 be3414b139a547a7009674de1b98e687
BLAKE2b-256 435de273e8fc080cb247ed06e29eb33e467c8260cb02a4d2f7e0484a87039544

See more details on using hashes here.

Supported by

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