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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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