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-2.0.1.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: lambda-handlers-2.0.1.tar.gz
  • Upload date:
  • Size: 17.0 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-2.0.1.tar.gz
Algorithm Hash digest
SHA256 cd5707ce531c3070d73ad9120b90d51347c15e4797bce1f4babd4323af471e78
MD5 e82ae3d9d1499dacf8a98442ea0f5d44
BLAKE2b-256 b8010278a43bb53c063620a4b30810642ee4aa794f6176251e69ec81eb3ccf30

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lambda_handlers-2.0.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-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 01d4c94132a016d541df13f423fdf98dbdca8b1d17aaa5e916decb3bcbe8f400
MD5 cc5eebeec66e62ce7db1ad3795f31faf
BLAKE2b-256 66003e2f0b9eb920a094bf0aff13bbdddb4a577f1c8657976a3a4c2f24324536

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