Skip to main content

Run lambda function in python on local machine.

Project description

Run lambda function on local machine

Prepare development environment

Please use a newly created virtualenv for python2.7.

Installation

Within virtualenv, run the following command.

$ cd $PROJECT_ROOT
$ pip install ./

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

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

usage: python-lambda-local [-h] [-l LIBRARY_PATH] [-f HANDLER_FUNCTION]
                           [-t TIMEOUT]
                           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

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

In 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 install in the directory lib. The test event of json format is in event.json file.

Content of test.py:

from rx import Observable


def handler(event, context):
    xs = Observable.from_([1, 2, 3, 4, 5, 6])
    ys = xs.to_blocking()
    zs = (x*x for x in ys if x > 3)
    for x in zs:
        print x

Content of event.json:

{
  "key": "value"
}

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:

[INFO 2015-10-16 18:21:14,774] Event: {'key': 'value'}
[INFO 2015-10-16 18:21:14,774] START RequestId: 324cb1c5-fa9b-4f39-8ad9-01c95f7d5744
16
25
36
[INFO 2015-10-16 18:21:14,775] END RequestId: 324cb1c5-fa9b-4f39-8ad9-01c95f7d5744
[INFO 2015-10-16 18:21:14,775] RESULT: None

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

Uploaded Source

Built Distribution

python_lambda_local-0.1.1-py2-none-any.whl (8.1 kB view details)

Uploaded Python 2

File details

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

File metadata

File hashes

Hashes for python-lambda-local-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7f947405325f34babd0e34ef250815b31ca2e5543212f97f1aee6b6808f5c570
MD5 e22a24d9d1913f992c0b4119fe5760b6
BLAKE2b-256 8d2a4802f984b494ad993b552f6b01d1415ae1f0b3dc02ebd2872c333d716292

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for python_lambda_local-0.1.1-py2-none-any.whl
Algorithm Hash digest
SHA256 127d36ca930990c0a2db8e7300c089960cd9d9151135ab03f07f7fcdba4fabd3
MD5 9d8cd9c1d3b9b268a28f32ff3f5447b7
BLAKE2b-256 747cf5aeffe12693b743df32657c9775037ab4461badb20aea1f817b9838e5ef

See more details on using hashes here.

Provenance

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