使用Serverless和python 3.7在AWS Lambda中打包dlib

问题描述 投票:0回答:1

我正在尝试使用无服务器框架和python3.7运行时来部署AWS Lambda函数,我需要将dlib打包为依赖项并导入到lambda函数内部。有什么想法是使dlib在python3.7上工作并使用无服务器框架成功打包的最简单方法是什么?TIA ...

amazon-web-services aws-lambda python-3.7 serverless-framework dlib
1个回答
0
投票

Marcin的建议行之有效,但有些乏味。幸运的是,无服务器框架来了。以下示例使用Python 3.8,但可以轻松将其切换为3.7。

先决条件:

serverless.yml

service: dlib-example

provider:
  name: aws
  runtime: python3.8

functions:
  dlib:
    handler: handler.main
    layers:
      - {Ref: PythonRequirementsLambdaLayer}

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: non-linux
    layer: true

requirements.txt

dlib==19.19.0

handler.py

import dlib


def main(event, context):
    print(dlib.__version__)


if __name__ == "__main__":
    main('', '')

然后使用sls deploy构建依赖关系(在Docker容器中)并使用CloudFormation部署到AWS。

测试

运行sls invoke -f dlib --log,您将得到类似这样的内容:

null
--------------------------------------------------------------------
START RequestId: 9fba7253-2f3b-425f-a0b7-9ee3dfaec13b Version: $LATEST
19.19.0
END RequestId: 9fba7253-2f3b-425f-a0b7-9ee3dfaec13b
REPORT RequestId: 9fba7253-2f3b-425f-a0b7-9ee3dfaec13b  Duration: 1.66 ms   Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 62 MB  Init Duration: 227.31 ms

干杯!

© www.soinside.com 2019 - 2024. All rights reserved.