Lồng nhau nếu lambda python

Hiện tại, tôi sử dụng AWS Lambda cho hầu hết tất cả các dự án của mình - từ ứng dụng Flask và bot Slack cho đến các công cụ giám sát và công việc định kỳ. Tôi thích việc triển khai thứ gì đó có giá trị rẻ và dễ dàng như thế nào

Python là ngôn ngữ yêu thích của tôi, nhưng việc xử lý các gói Python trong Lambda có thể khó khăn. Nhiều gói quan trọng cần biên dịch các tiện ích mở rộng C, như psycopg2 để truy cập Postgres hoặc numpy, scipy, pandas hoặc sklearn để phân tích số. Nếu biên dịch chúng trên hệ thống Mac hoặc Windows, bạn sẽ gặp lỗi khi Lambda cố tải chúng

Đường dẫn nhập khẩu cũng cần sự khéo léo. Bạn có thể cài đặt các phụ thuộc trực tiếp vào thư mục cấp cao nhất của mình, nhưng điều đó làm lộn xộn không gian làm việc của bạn. Nếu bạn cài đặt chúng vào một thư mục con như

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
2 hoặc
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
3, bạn sẽ phải làm phiền với
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
4 của mình khi bắt đầu chức năng của mình

Nhưng có một cách tốt hơn nhiều. Trong bài đăng này, tôi sẽ chỉ cho bạn cách sử dụng plugin

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
5 cho Serverless Framework

Thiết lập ban đầu

Hãy chuẩn bị sẵn sàng cho môi trường của chúng ta. Nếu bạn đã cài đặt Node và NPM, hãy cài đặt Serverless Framework trên toàn cầu với

$ npm install -g serverless

Bạn cũng sẽ cần định cấu hình môi trường của mình bằng thông tin đăng nhập AWS

Ghi chú. nếu bạn cần xem lại cách cài đặt Framework hoặc nhận thông tin đăng nhập AWS, hãy xem phần Điều kiện tiên quyết ở đầu Hướng dẫn bắt đầu nhanh của chúng tôi

Tạo dịch vụ của bạn tại địa phương

Đối với bản trình diễn nhanh này, chúng tôi sẽ triển khai hàm Lambda sử dụng gói NumPy phổ biến

Chúng tôi có thể tạo một dịch vụ từ một mẫu. Tôi sẽ sử dụng Python 3, nhưng nó cũng hoạt động với Python 2

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test

Thao tác này sẽ tạo một dự án mẫu Serverless Python 3 tại đường dẫn đã cho [

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
6] với tên dịch vụ là
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
7. Bạn sẽ cần thay đổi thư mục đó và tạo một môi trường ảo để phát triển cục bộ

[Ghi chú. đọc thêm tại đây về cách thức và lý do sử dụng môi trường ảo với Python. ]

________số 8

Hãy thiết lập chức năng chúng tôi muốn triển khai. Chỉnh sửa nội dung của

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
8 để nó chứa nội dung sau

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
0

Đây là một chức năng cực kỳ đơn giản sử dụng một ví dụ từ NumPy Quick Start. Khi làm việc với Lambda, bạn sẽ cần xác định một hàm chấp nhận hai đối số.

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
9 và
$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
0. Bạn có thể đọc thêm tại AWS về Trình xử lý hàm Lambda cho Python

Lưu ý hai dòng cuối cùng của tệp, cung cấp cho chúng tôi cách nhanh chóng kiểm tra chức năng cục bộ. Nếu chúng ta chạy

$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
1, nó sẽ chạy hàm
$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
2 của chúng ta. Hãy cho nó một shot

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
5

À, chúng tôi chưa cài đặt

$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
3 trong môi trường ảo của chúng tôi. Hãy làm điều đó ngay bây giờ và lưu các phiên bản gói của môi trường của chúng ta vào tệp
$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
4

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
8

Nếu chúng ta chạy lệnh cục bộ ngay bây giờ, chúng ta sẽ thấy đầu ra mà chúng ta muốn

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
9

Hoàn hảo

Triển khai dịch vụ của bạn

Hàm của chúng tôi đang hoạt động cục bộ và đã sẵn sàng để chúng tôi triển khai lên Lambda. Chỉnh sửa tệp

$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
5 để trông giống như sau

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
1

Đây là một dịch vụ cơ bản được gọi là

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
7. Nó sẽ triển khai một Python 3 duy nhất. 6 có tên là
$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
3 tới AWS và điểm bắt đầu cho hàm
$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
3 là hàm
$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
9 trong mô-đun
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
8

Bước cuối cùng của chúng tôi trước khi triển khai là thêm plugin

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
5. Tạo một gói. json để lưu các phụ thuộc nút của bạn. Chấp nhận các giá trị mặc định, sau đó cài đặt plugin

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
8

Để định cấu hình tệp

$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
5 của chúng tôi để sử dụng plugin, chúng tôi sẽ thêm các dòng sau vào tệp
$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
5 của chúng tôi

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
1

Ghi chú. một phiên bản trước của bài viết này thiết lập

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
04 thay vì
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
05. Bạn sẽ cần
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
5 v3. 0. 5 hoặc cao hơn cho tùy chọn này

Bạn cần cài đặt Docker để có thể thiết lập

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
04 hoặc
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
05. Ngoài ra, bạn có thể đặt
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
09 và nó sẽ không sử dụng gói Docker. Tuy nhiên, đóng gói Docker là điều cần thiết nếu bạn cần xây dựng các gói gốc là một phần của các phần phụ thuộc của bạn như Psycopg2, NumPy, Pandas, v.v.

Phần

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
50 đăng ký plugin với Framework. Trong phần
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
51, chúng tôi yêu cầu plugin sử dụng Docker khi cài đặt các gói bằng pip. Nó sẽ sử dụng bộ chứa Docker tương tự như môi trường Lambda để các tiện ích mở rộng được biên dịch sẽ tương thích. Bạn sẽ cần cài đặt Docker để làm việc này

Plugin hoạt động bằng cách nối vào Framework trên lệnh triển khai. Trước khi gói của bạn được nén, nó sử dụng Docker để cài đặt các gói được liệt kê trong tệp

$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
4 của bạn và lưu chúng vào thư mục
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
53. Sau đó, nó liên kết tượng trưng nội dung của
$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
53 vào thư mục cấp cao nhất của bạn để quá trình nhập Python hoạt động như mong đợi. Sau khi triển khai xong, nó sẽ dọn sạch các liên kết tượng trưng để giữ cho thư mục của bạn sạch sẽ

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
0

Tuyệt quá. Hãy gọi hàm

$ cd numpy-test
$ virtualenv venv --python=python3
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/username/scratch/numpy-test/venv/bin/python3.6
Also creating executable in /Users/username/scratch/numpy-test/venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate
[venv] $
3 của chúng ta và đọc nhật ký

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
1

Và nó đây rồi. Bạn đã có NumPy trong Lambda của mình

Hãy chắc chắn kiểm tra repo để biết chức năng bổ sung, bao gồm tự động nén thư viện trước khi triển khai, điều này có thể giúp ích rất nhiều cho các thư viện số lớn hơn trong Python

Xin chân thành cảm ơn nhóm United Income và đặc biệt là Daniel Schep đã tạo ra gói

$ serverless create \
  --template aws-python3 \
  --name numpy-test \
  --path numpy-test
5. Nếu bạn muốn làm việc toàn thời gian không có máy chủ, hãy xem United Income. Họ sử dụng kiến ​​trúc 100% không có máy chủ cho mọi thứ, từ phục vụ ứng dụng web của họ đến chạy hàng triệu mô phỏng tài chính và họ luôn tìm kiếm các kỹ sư tài năng để tham gia vào nhóm đang phát triển của họ ở Washington, DC

Chủ Đề