@@ -18,43 +18,45 @@ Within virtualenv, run the following command.
1818
1919.. code :: bash
2020
21- $ pip install python-lambda-local
21+ $ pip install python-lambda-local
2222
2323 This will install the package with name ``python-lambda-local `` in the
2424virtualenv. Now you can use the command ``python-lambda-local `` to run
2525your AWS Lambda function written in Python on your own machine.
2626
27- Usage
28- -----
27+ Usage as a shell command
28+ ------------------------
2929
3030Run ``python-lambda-local -h `` to see the help.
3131
3232::
3333
34- usage: python-lambda-local [-h] [-l LIBRARY_PATH] [-f HANDLER_FUNCTION]
35- [-t TIMEOUT] [-a ARN_STRING] [-v VERSION_NAME]
36- [--version]
37- FILE EVENT
38-
39- Run AWS Lambda function written in Python on local machine.
40-
41- positional arguments:
42- FILE lambda function file name
43- EVENT event data file name
44-
45- optional arguments:
46- -h, --help show this help message and exit
47- -l LIBRARY_PATH, --library LIBRARY_PATH
48- path of 3rd party libraries
49- -f HANDLER_FUNCTION, --function HANDLER_FUNCTION
50- lambda function handler name, default: "handler"
51- -t TIMEOUT, --timeout TIMEOUT
52- seconds until lambda function timeout, default: 3
53- -a ARN_STRING, --arn-string ARN_STRING
54- ARN string for lambda function
55- -v VERSION_NAME, --version-name VERSION_NAME
56- lambda function version name
57- --version print the version of python-lambda-local and exit
34+ usage: python-lambda-local [-h] [-l LIBRARY_PATH] [-f HANDLER_FUNCTION]
35+ [-t TIMEOUT] [-a ARN_STRING] [-v VERSION_NAME]
36+ [-e ENVIRONMENT_VARIABLES] [--version]
37+ FILE EVENT
38+
39+ Run AWS Lambda function written in Python on local machine.
40+
41+ positional arguments:
42+ FILE lambda function file name
43+ EVENT event data file name
44+
45+ optional arguments:
46+ -h, --help show this help message and exit
47+ -l LIBRARY_PATH, --library LIBRARY_PATH
48+ path of 3rd party libraries
49+ -f HANDLER_FUNCTION, --function HANDLER_FUNCTION
50+ lambda function handler name, default: "handler"
51+ -t TIMEOUT, --timeout TIMEOUT
52+ seconds until lambda function timeout, default: 3
53+ -a ARN_STRING, --arn-string ARN_STRING
54+ ARN string for lambda function
55+ -v VERSION_NAME, --version-name VERSION_NAME
56+ lambda function version name
57+ -e ENVIRONMENT_VARIABLES, --environment-variables ENVIRONMENT_VARIABLES
58+ path to flat json file with environment variables
59+ --version print the version of python-lambda-local and exit
5860
5961Prepare development directory
6062~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -66,25 +68,25 @@ Suppose your project directory is like this:
6668
6769::
6870
69- ├── event.json
70- ├── lib
71- │ ├── rx
72- │ │ ├── abstractobserver.py
73- │ │ ├── ... (package content of rx)
74- ...
75- │ │ └── testscheduler.py
76- │ └── Rx-1.2.3.dist-info
77- │ ├── DESCRIPTION.rst
78- │ ├── METADATA
79- │ ├── metadata.json
80- │ ├── pbr.json
81- │ ├── RECORD
82- │ ├── top_level.txt
83- │ ├── WHEEL
84- │ └── zip-safe
85- └── test.py
86-
87- The handler' s code is in ``test.py `` and the function name of the
71+ ├── event.json
72+ ├── lib
73+ │ ├── rx
74+ │ │ ├── abstractobserver.py
75+ │ │ ├── ... (package content of rx)
76+ ...
77+ │ │ └── testscheduler.py
78+ │ └── Rx-1.2.3.dist-info
79+ │ ├── DESCRIPTION.rst
80+ │ ├── METADATA
81+ │ ├── metadata.json
82+ │ ├── pbr.json
83+ │ ├── RECORD
84+ │ ├── top_level.txt
85+ │ ├── WHEEL
86+ │ └── zip-safe
87+ └── test.py
88+
89+ The handler’ s code is in ``test.py `` and the function name of the
8890handler is ``handler ``. The source depends on 3rd party library ``rx ``
8991and it is installed in the directory ``lib ``. The test event in json
9092format is in ``event.json `` file.
@@ -94,25 +96,25 @@ Content of ``test.py``:
9496
9597.. code :: python
9698
97- from __future__ import print_function
98- from rx import Observable
99+ from __future__ import print_function
100+ from rx import Observable
99101
100102
101- def handler (event , context ):
102- xs = Observable.from_(range (event[' answer' ]))
103- ys = xs.to_blocking()
104- zs = (x* x for x in ys if x % 7 == 0 )
105- for x in zs:
106- print (x)
103+ def handler (event , context ):
104+ xs = Observable.from_(range (event[' answer' ]))
105+ ys = xs.to_blocking()
106+ zs = (x* x for x in ys if x % 7 == 0 )
107+ for x in zs:
108+ print (x)
107109
108110 Content of ``event.json ``:
109111^^^^^^^^^^^^^^^^^^^^^^^^^^
110112
111113.. code :: json
112114
113- {
114- "answer" : 42
115- }
115+ {
116+ "answer" : 42
117+ }
116118
117119 Run the lambda function
118120^^^^^^^^^^^^^^^^^^^^^^^
@@ -122,24 +124,63 @@ the following command
122124
123125::
124126
125- python-lambda-local -l lib/ -f handler -t 5 test.py event.json
127+ python-lambda-local -l lib/ -f handler -t 5 test.py event.json
126128
127129The output will be like:
128130
129131::
130132
131- [root - INFO - 2017-04-19 12:39:05,512] Event: {u'answer': 42}
132- [root - INFO - 2017-04-19 12:39:05,512] START RequestId: b918f9ae-0ca1-44af-9937-dd5f9eeedcc1
133- 0
134- 49
135- 196
136- 441
137- 784
138- 1225
139- [root - INFO - 2017-04-19 12:39:05,515] END RequestId: b918f9ae-0ca1-44af-9937-dd5f9eeedcc1
140- [root - INFO - 2017-04-19 12:39:05,515] RESULT:
141- None
142- [root - INFO - 2017-04-19 12:39:05,515] REPORT RequestId: b918f9ae-0ca1-44af-9937-dd5f9eeedcc1 Duration: 2.27 ms
133+ [root - INFO - 2018-11-20 17:10:53,352] Event: {'answer': 42}
134+ [root - INFO - 2018-11-20 17:10:53,352] START RequestId: 3c8e6db4-886a-43da-a1c7-5e6f715de531 Version:
135+ 0
136+ 49
137+ 196
138+ 441
139+ 784
140+ 1225
141+ [root - INFO - 2018-11-20 17:10:53,359] END RequestId: 3c8e6db4-886a-43da-a1c7-5e6f715de531
142+ [root - INFO - 2018-11-20 17:10:53,360] REPORT RequestId: 3c8e6db4-886a-43da-a1c7-5e6f715de531 Duration: 2.17 ms
143+ [root - INFO - 2018-11-20 17:10:53,360] RESULT:
144+ None
145+
146+ Usage as a library
147+ ------------------
148+
149+ API signature
150+ ~~~~~~~~~~~~~
151+
152+ .. code :: python
153+
154+ call(func, event, context, environment_variables = {})
155+
156+ Call a handler function ``func `` with given ``event ``, ``context `` and
157+ custom ``environment_variables ``.
158+
159+ Sample
160+ ~~~~~~
161+
162+ 1. Make sure the 3rd party libraries used in the AWS Lambda function can
163+ be imported.
164+
165+ .. code :: bash
166+
167+ pip install rx
168+
169+ 2. To call the lambda function above with your python code:
170+
171+ .. code :: python
172+
173+ from lambda_local.main import call
174+ from lambda_local.context import Context
175+
176+ import test
177+
178+ event = {
179+ " answer" : 42
180+ }
181+ context = Context(5 )
182+
183+ call(test.handler, event, context)
143184
144185 .. |Join the chat at https://gitter.im/HDE/python-lambda-local | image :: https://badges.gitter.im/Join%20Chat.svg
145186 :target: https://gitter.im/HDE/python-lambda-local?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
0 commit comments