Skip to main content

Python Handler Helper for Lambda

Project description

Documentation Status https://travis-ci.com/lhgomes/phhelper.svg?branch=master https://badge.fury.io/py/phhelper.svg

Overview

Simplify best practice for Lambda in Python, handling Lambda events and errors with detailed and standarized logs.

Features

  • Dead simple to use, reduces the complexity of writing a Lambda with Python runtime

  • Guarantees that Source will get a response even if an exception is raised

  • Sends meaningful errors to Cloudwatch in the case of a failure

  • Threading enables best runtime performance for events with multiple records

  • JSON logging that includes request id’s, event id’s and source to assist in tracing logs relevant to a particular event

Installation

Install into the root folder of your lambda function

cd my-lambda-function/
pip install phhelper -t .

Example Usage

from phhelper import aws_lambda_helper
import json
import time

@aws_lambda_helper.handler
def lambda_handler(event, context):
    context.logging.info('info_test')
    time.sleep(0.5)
    context.logging.error('error_test')
    time.sleep(0.5)
    context.logging.debug('debug_test')

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Threading

If the event source send multiple records to be processed, you can enable Multithreading processing, by creating a Environment variable called THREADING_ENABLED with value TRUE. This will make a loop into event records, starting a thread for each record. Your handler will receive each record in a separeted call, inside a thread model.

Logging

You can define the general log verbosity level using a Environment variable called LOG_LEVEL and the boto3 log level using a Environment variable called BOTO_LOG_LEVEL.

The valid values for both Environment variables are: * DEBUG * INFO * WARNING * ERROR * CRITICAL

The default values are:

LOG_LEVEL = 'ERROR'
BOTO_LOG_LEVEL` = 'CRITICAL'

Credits

Decorator implementation inspired by https://github.com/aws-cloudformation/custom-resource-helper

Log implementation inspired by https://gitlab.com/hadrien/aws_lambda_logging

Multiprocessing implementation inspired by https://medium.com/@urban_institute/using-multiprocessing-to-make-python-code-faster-23ea5ef996ba

License

This library is licensed under the MIT License.

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

phhelper-0.9.18.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file phhelper-0.9.18.tar.gz.

File metadata

  • Download URL: phhelper-0.9.18.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7

File hashes

Hashes for phhelper-0.9.18.tar.gz
Algorithm Hash digest
SHA256 078fe3e833797d2fd421b9c9d5108dcb91f197b0e4b5453838a221f428cb5779
MD5 ff0524fc1fc87a5efc5d786671cbe112
BLAKE2b-256 ced4404d31fda5b162ea4f8451db7a3ef21097d096944f0ce6387d2bdc87ba0b

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