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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: lambda-handlers-2.0.0.tar.gz
  • Upload date:
  • Size: 16.9 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.0.tar.gz
Algorithm Hash digest
SHA256 2458e91be95bed33b5f9a535f125b9e9e5b2b1bb200517562ff482734e32c033
MD5 c062bc7566b1facfb2cffeb5fa04a73e
BLAKE2b-256 411632a79c8134363c793df7b682a6ff2f66648ea31cf0634d9420c2d4a829a5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lambda_handlers-2.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0bf0e2f0c1aa9194f6cd2fac1399506d96480ec8242f4f50745ff56a2368d9f2
MD5 7703693824932ed85dd1480bad76bde4
BLAKE2b-256 4390903f5dc3dea447cce85feae03d0bfe13c522d2d1f4fe191a045bbb547426

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