Skip to main content

Run lambda function in python on local machine.

Project description

Join the chat at https://gitter.im/HDE/python-lambda-local wercker status PyPI version

Run lambda function on local machine

Prepare development environment

Please use a newly created virtualenv of Python 2.7 or Python 3.7.

Installation

Within virtualenv, run the following command.

$ pip install python-lambda-local

This will install the package with name python-lambda-local in the virtualenv. Now you can use the command python-lambda-local to run your AWS Lambda function written in Python on your own machine.

Usage as a shell command

Run python-lambda-local -h to see the help.

usage: python-lambda-local [-h] [-l LIBRARY_PATH] [-f HANDLER_FUNCTION]
                           [-t TIMEOUT] [-a ARN_STRING] [-v VERSION_NAME]
                           [-e ENVIRONMENT_VARIABLES] [--version]
                           FILE EVENT

Run AWS Lambda function written in Python on local machine.

positional arguments:
  FILE                  lambda function file name
  EVENT                 event data file name

optional arguments:
  -h, --help            show this help message and exit
  -l LIBRARY_PATH, --library LIBRARY_PATH
                        path of 3rd party libraries
  -f HANDLER_FUNCTION, --function HANDLER_FUNCTION
                        lambda function handler name, default: "handler"
  -t TIMEOUT, --timeout TIMEOUT
                        seconds until lambda function timeout, default: 3
  -a ARN_STRING, --arn-string ARN_STRING
                        ARN string for lambda function
  -v VERSION_NAME, --version-name VERSION_NAME
                        lambda function version name
  -e ENVIRONMENT_VARIABLES, --environment-variables ENVIRONMENT_VARIABLES
                        path to flat json file with environment variables
  --version             print the version of python-lambda-local and exit

Prepare development directory

Project directory structure

Suppose your project directory is like this:

├── event.json
├── lib
│   ├── rx
│   │   ├── abstractobserver.py
│   │   ├── ... (package content of rx)
...
│   │       └── testscheduler.py
│   └── Rx-1.2.3.dist-info
│       ├── DESCRIPTION.rst
│       ├── METADATA
│       ├── metadata.json
│       ├── pbr.json
│       ├── RECORD
│       ├── top_level.txt
│       ├── WHEEL
│       └── zip-safe
└── test.py

The handler’s code is in test.py and the function name of the handler is handler. The source depends on 3rd party library rx and it is installed in the directory lib. The test event in json format is in event.json file.

Content of test.py:

from __future__ import print_function
from rx import Observable


def handler(event, context):
    xs = Observable.from_(range(event['answer']))
    ys = xs.to_blocking()
    zs = (x*x for x in ys if x % 7 == 0)
    for x in zs:
        print(x)

Content of event.json:

{
  "answer": 42
}

Run the lambda function

Within the project root directory, you can run the lambda function with the following command

python-lambda-local -l lib/ -f handler -t 5 test.py event.json

The output will be like:

[root - INFO - 2018-11-20 17:10:53,352] Event: {'answer': 42}
[root - INFO - 2018-11-20 17:10:53,352] START RequestId: 3c8e6db4-886a-43da-a1c7-5e6f715de531 Version:
0
49
196
441
784
1225
[root - INFO - 2018-11-20 17:10:53,359] END RequestId: 3c8e6db4-886a-43da-a1c7-5e6f715de531
[root - INFO - 2018-11-20 17:10:53,360] REPORT RequestId: 3c8e6db4-886a-43da-a1c7-5e6f715de531  Duration: 2.17 ms
[root - INFO - 2018-11-20 17:10:53,360] RESULT:
None

Usage as a library

API signature

call(func, event, context, environment_variables={})

Call a handler function func with given event, context and custom environment_variables.

Sample

  1. Make sure the 3rd party libraries used in the AWS Lambda function can be imported.

pip install rx
  1. To call the lambda function above with your python code:

from lambda_local.main import call
from lambda_local.context import Context

import test

event = {
    "answer": 42
}
context = Context(5)

call(test.handler, event, context)

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

python-lambda-local-0.1.11.tar.gz (9.0 kB view details)

Uploaded Source

Built Distributions

python_lambda_local-0.1.11-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

python_lambda_local-0.1.11-py2-none-any.whl (9.0 kB view details)

Uploaded Python 2

File details

Details for the file python-lambda-local-0.1.11.tar.gz.

File metadata

  • Download URL: python-lambda-local-0.1.11.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0

File hashes

Hashes for python-lambda-local-0.1.11.tar.gz
Algorithm Hash digest
SHA256 1ba6d265383b46a9bef212e25ea93bcbba9b5d99a51f2f143e21338dfe7c1b88
MD5 07c6b625a3afcb2583b9795bee5617bf
BLAKE2b-256 b6b944348c120864e89045a8eb3ec748b626e123b564eca06dae0e8e8827f5c3

See more details on using hashes here.

File details

Details for the file python_lambda_local-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: python_lambda_local-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0

File hashes

Hashes for python_lambda_local-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 c9d889469cb0a3a3bc4d945a3797db24c2211b5629ae5c71631e8b84b73004c1
MD5 3715ba81ecf8581213882edaaa7a3aef
BLAKE2b-256 f3fa7229b5efa757c618a6135c6cb0f6efe3dd0c86476f635a8c011f04e2caee

See more details on using hashes here.

File details

Details for the file python_lambda_local-0.1.11-py2-none-any.whl.

File metadata

  • Download URL: python_lambda_local-0.1.11-py2-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0

File hashes

Hashes for python_lambda_local-0.1.11-py2-none-any.whl
Algorithm Hash digest
SHA256 5562c2490b968714be735c27a17ff5b7be78309560a944b868637511328e94a9
MD5 1b80750dc3cd022b031d54f4d0cc8598
BLAKE2b-256 80af9b629b67c95f94cbfe1cbf46339558f5f83d76c9390387396be0220f6f4b

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