Package: python-lambda-local
Run lambda function on local machinePrepare development environmentPlease use a newly created virtualenv of Python 3.7+.InstallationWithin virtualenv, run the following command.$pipinstallpython-lambda-localThis will install the package with namepython-lambda-localin the
virtualenv. Now you can use the commandpython-lambda-localto run
your AWS Lambda function written in Python on your own machine.Usage as a shell commandRunpython-lambda-local-hto 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 exitPrepare development directoryProject directory structureSuppose your project directory is like this:├── event.json
├── lib
│   ├── rx
│   │   ├── abstractobserver.py
│   │   ├── ... (package content of rx)
...
│   │       └── testscheduler.py
│   └── Rx-1.6.1.dist-info
│       ├── DESCRIPTION.rst
│       ├── METADATA
│       ├── metadata.json
│       ├── pbr.json
│       ├── RECORD
│       ├── top_level.txt
│       ├── WHEEL
│       └── zip-safe
└── test.pyThe handler’s code is intest.pyand the function name of the
handler ishandler. The source depends on 3rd party libraryrxand it is installed in the directorylib. The test event in json
format is inevent.jsonfile.Content oftest.py:from__future__importprint_functionfromrximportObservabledefhandler(event,context):xs=Observable.from_(range(event['answer']))ys=xs.to_blocking()zs=(x*xforxinysifx%7==0)forxinzs:print(x)Content ofevent.json:{"answer":42}Run the lambda functionWithin the project root directory, you can run the lambda function with
the following commandpython-lambda-local -l lib/ -f handler -t 5 test.py event.jsonThe 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:
NoneUsage as a libraryAPI signaturecall(func,event,context,environment_variables={})Call a handler functionfuncwith givenevent,contextand
customenvironment_variables.SampleMake sure the 3rd party libraries used in the AWS Lambda function can
be imported.pipinstallrx==1.6.1To call the lambda function above with your python code:fromlambda_local.mainimportcallfromlambda_local.contextimportContextimporttestevent={"answer":42}context=Context(5)call(test.handler,event,context)