Skip to main content

Simple HTTP server to invoke a Lambda function locally

Project description

Lambda Gateway

pypi python pytest coverage maintainability

Test AWS Lambda functions invoked as API Gateway proxy integrations locally.

This tool extends the native Python SimpleHTTPRequestHandler to proxy requests to a local Lambda function using the ThreadingHTTPServer.

This tool is was specifically implemented to use the standard Python library only. No additional pip installation is required.

After installing, navigate to the directory where your Lambda function is defined and invoke it with the CLI tool using the configured handler, eg:

lambda-gateway [-p PORT] [-t SECONDS] lambda_function.lambda_handler
# => Serving HTTP on :: port 8000 (http://[::]:8000/) ...

Installation

Install with pip.

pip install lambda-gateway

Usage

Create a Lambda function handler in Python 3

# ./lambda_function.py
import json


def lambda_handler(event, context):
    return {
        'body': json.dumps({'text': 'Hello from Lambda Gateway!'}),
        'statusCode': 200,
        'headers': {
            'Content-Type': 'application/json',
        },
    }

Start a local server with the signature of your Lambda handler as the argument.

Note — the handler must be importable from the current working directory

lambda-gateway [-B PATH] [-b ADDR] [-p PORT] [-t SECONDS] lambda_function.lambda_handler
# => Starting LambdaRequestHandler at http://localhost:8000/

Test the function with cURL.

curl http://localhost:8000/?name=Pythonista
# => {"text": "Hello, Pythonista! ~ Lambda Gateway"}

Timeouts

API Gateway imposes a 30 second timeout on Lambda responses. This constraint is implemented in this project using Python's async/await syntax.

The timeout length can be adjusted using the -t / --timeout CLI option.

lambda-gateway -t 3 lambda_function.lambda_handler

API Gateway Payloads

API Gateway supports two versions of proxied JSON payloads to Lambda integrations, 1.0 and 2.0.

Versions 0.8+ of Lambda Gateway use 2.0 by default, but this can be changed at startup time using the -V / --payload-version option:

lambda-gateway -V1.0 lambda_function.lambda_handler

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-gateway-0.8.0.tar.gz (20.0 kB view details)

Uploaded Source

File details

Details for the file lambda-gateway-0.8.0.tar.gz.

File metadata

  • Download URL: lambda-gateway-0.8.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.9

File hashes

Hashes for lambda-gateway-0.8.0.tar.gz
Algorithm Hash digest
SHA256 f8bf888097bb7f408c239b77bf0b2cff8bcd3d2898415ea116bea259403772d6
MD5 dfa7cc500c79f9c1e5a17399604a2bbd
BLAKE2b-256 df231bed1026011be323f4f4ca48f77c28e65dcc0dbfb39f8f1471c495b8d9e0

See more details on using hashes here.

Supported by

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