Chạy chức năng Lambda trên máy cục bộ
Chuẩn bị môi trường phát triển
Vui lòng sử dụng Virtualenv mới được tạo của Python 3.7+.
Cài đặt
Trong VirtualEnv, chạy lệnh sau.
$ pip install python-lambda-local
Điều này sẽ cài đặt gói với tên Python-Lambda-Local trong ảo. Bây giờ bạn có thể sử dụng lệnh Python-Lambda-Local để chạy chức năng AWS Lambda của bạn được viết bằng Python trên máy của riêng bạn.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.
Sử dụng như một lệnh shell
Chạy Python-Lambda-Local-h để xem sự giúp đỡ.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
Chuẩn bị thư mục phát triển
Cấu trúc thư mục dự án
Giả sử thư mục dự án của bạn là như thế này:
├── 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.py
Mã xử lý có trong Test.py và tên hàm của trình xử lý là người xử lý. Nguồn phụ thuộc vào thư viện bên thứ 3 RX và nó được cài đặt trong thư mục lib. Sự kiện thử nghiệm ở định dạng JSON nằm trong tệp event.json.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.
Nội dung của Test.py: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]
Nội dung của event.json:event.json:
{
"answer": 42
}
Chạy chức năng Lambda
Trong thư mục gốc dự án, bạn có thể chạy chức năng Lambda với lệnh sau
python-lambda-local -l lib/ -f handler -t 5 test.py event.json
Đầu ra sẽ giống như:
[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 [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
Sử dụng như một thư viện
Chữ ký API
call[func, event, context, environment_variables={}]
Gọi một hàm Handler Func với sự kiện đã cho, bối cảnh và môi trường tùy chỉnh_varia.func with given event, context and custom environment_variables.
Vật mẫu
Đảm bảo có thể nhập các thư viện của bên thứ 3 trong hàm AWS Lambda.
pip install rx==1.6.1
Để gọi chức năng Lambda ở trên với mã Python của bạn:
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]