Python 3 和 virtualenv 的 AWS Lambda 无法安装依赖项

AWS Lambda with Python 3 and virtualenv failed to install dependencies

我在 Python3.6 从事一个项目,我使用 AWS Lambda 实现 python 中的一些功能。我已经将 buildspec.yml 文件写入 "build" 并将我的函数从存储库部署到 lambda 函数。这是代码:

version: 0.2
phases:
 install:
   commands:
     - echo "install step"
     - apt-get update
     - apt-get install zip -y
     - apt-get install python3-pip -y
 pre_build:
   commands:
     - echo "pre_build step"
     - pip install --upgrade pip
     - pip install --upgrade awscli
     - pip install --upgrade virtualenv
     # Define directories
     - export HOME_DIR=`pwd`
     - export PREPROCESSING_DIR=$HOME_DIR/preprocessing
     - export COMPARE_DIR=$HOME_DIR/compareHilightGood
     - export LAUNCH_HILIGHT_DIR=$HOME_DIR/LaunchHiLight
     - export NLTK_DATA=$HOME_DIR/nltk_data
     - mkdir nltk_data
     # create virtual environements
     - cd $HOME_DIR
     - virtualenv venv_preprocessing
     - virtualenv venv_compare
     - export SITE_PACKAGE_PREPROCESSING=$HOME_DIR/venv_preprocessing/lib/python3.6/site-packages
     - export SITE_PACKAGE_COMPARE=$HOME_DIR/venv_compare/lib/python3.6/site-packages
 build:
   commands:
     - echo "build step"
     - cd $HOME_DIR
     # Configure preprocessing virtual environement
     - . venv_preprocessing/bin/activate
       pip install requests
       pip install nltk
       python -m nltk.downloader -d $NLTK_DATA wordnet stopwords punkt
       deactivate
     - mv $NLTK_DATA $SITE_PACKAGE_PREPROCESSING
     - mv $PREPROCESSING_DIR/* $SITE_PACKAGE_PREPROCESSING
     - cd $SITE_PACKAGE_PREPROCESSING
     - sudo zip -r9 -q $HOME_DIR/preprocessing.zip .
     # Configure compare virtual environement
     - cd $HOME_DIR
     - . venv_compare/bin/activate
       pip install gensim
       pip install pandas
       deactivate
     - mv $COMPARE_DIR/* $SITE_PACKAGE_COMPARE
     - cd $SITE_PACKAGE_COMPARE
     - sudo zip -r9 -q $HOME_DIR/compare.zip .
     # Launch hilight
     - cd $LAUNCH_HILIGHT_DIR
     - sudo zip -r9 -q $HOME_DIR/launchHilight.zip .
 post_build:
   commands:
     - echo "post_build step"
     - cd $HOME_DIR
     - ls
     # preprocessing
     - aws s3 rm s3://lambda-preprocessing --recursive
     - aws s3 cp --acl public-read preprocessing.zip s3://lambda-preprocessing/preprocessing.zip
     - aws lambda update-function-code --function-name arn:aws:lambda:eu-west-3:671560023774:function:preprocessing --s3-bucket lambda-preprocessing --s3-key preprocessing.zip
     - aws lambda update-function-configuration --function-name arn:aws:lambda:eu-west-3:671560023774:function:preprocessing --environment 'Variables={NLTK_DATA=/var/task/nltk_data}'
     # compare hilight good
     - aws s3 rm s3://lambda-comparehilightgood --recursive
     - aws s3 cp --acl public-read compare.zip s3://lambda-comparehilightgood/compare.zip
     - aws lambda update-function-code --function-name arn:aws:lambda:eu-west-3:671560023774:function:compareHilightGood --s3-bucket lambda-comparehilightgood --s3-key compare.zip
     # launchHilight
     - aws s3 rm s3://hilightalgo --recursive
     - aws s3 cp --quiet --acl public-read launchHilight.zip s3://hilightalgo/launchHilight.zip
     - aws lambda update-function-code --function-name arn:aws:lambda:eu-west-3:671560023774:function:LaunchHilight --s3-bucket hilightalgo --s3-key launchHilight.zip
artifacts:
  files:
    - '**/*'

在这个构建过程中,我创建了两个 virtualenv,将我的依赖项安装到它们中,然后我压缩了我的 lambda 部署包,这些包由以下内容组成:

  1. virtualenv 的站点包
  2. 来源

之后,我将我的 zip 包存储到 S3 存储桶中,并使用 aws cli 更新函数的代码。一切似乎都很好,但我有两个问题:

首先,这些文件对我来说太轻了 (3.8MB)。当我想测试我的 lambda 函数时,就像没有安装任何模块一样。请参阅以下错误:

Unable to import module 'lambda_function': No module named 'gensim'

我认为 virtualenv 没有安装模块,因为当我下载 .zip 文件时,我可以看到 __pycache__ 文件夹只包含一个小的 easy_install.cpython-36.pyc.

我不知道我做错了什么,但我认为问题出在我的部署包上。有人有什么想法吗?

您是否在您的 virtualenv 中压缩了 lib 和 lib64 的站点包?我注意到有些包裹在一个地方或另一个地方结束,我必须从两个地方打包它们。

看看是否可以找到 gensim 是否安装在 lib/... 或 lib64/... 中的站点包中

如果您要部署到 AWS Lambda,最好使用 Serverless or Zappa 之类的框架将您的代码和依赖项打包到一个 zip 文件中,以便通过 S3 进行部署。

两者都有效,尽管我更喜欢无服务器,因为它是可插拔的,cross-language、似乎 得到更好的支持并且对我有用。 This post 很好地 运行 介绍了如何开始。

一旦无服务器打包工作,您的 buildspec.yml 变得非常简单:安装无服务器,运行 serverless package.

如果您愿意,您也可以使用 Serverless 为您管理 AWS 基础设施。但我更喜欢单独使用 Terraform.