使用Lambda在AWS Codestar中安装Python依赖项

时间:2018-07-07 14:52:51

标签: amazon-web-services aws-lambda aws-codestar

我正在尝试AWS Codestar。我的目标是部署一个非平凡的lambda终结点,即处理程序具有依赖项的地方。理想情况下,我希望能够在某个地方的requirements.txt文件中指定它们,但这似乎不是那么简单。具体来说,我想部署一个依赖于nltk的lambda处理程序,并在Codebuild流程的一部分中下载nltk标记程序“ punkt”的文件,并将其打包为Lambda。

如何通过buildspec.ymltemplate.yml完成此操作?在下面,我试图将pip依赖项安装到子目录lib并将其包含在zip工件中。

运行时,Codebuild能够安装依赖项,导入nltk并运行测试,成功部署到Lambda,并且正确的文件被打包在lib子文件夹中(我下载了ZIP文件进行检查)但我在Lambda日志中看到错误:unable to import module 'index': No module named 'nltk'

这是我的buildspec.yml

    version: 0.2

    phases:
      install:
        commands:
          - pip install -r requirements.txt -t lib
          # Upgrade AWS CLI to the latest version
          - pip install --upgrade awscli

      pre_build:
        commands:
          - python -V
          - export PYTHONPATH=$PYTHONPATH:./lib
          - export HOME_DIR=`pwd`
          - mkdir $HOME_DIR/nltk_data/
          - export NLTK_DATA=$HOME_DIR/nltk_data
          - python -m nltk.downloader -d $NLTK_DATA punkt
          - python -m unittest discover tests

      build:
        commands:
          - aws cloudformation package --template template.yml --s3-bucket 
$S3_BUCKET --output-template template-export.yml

    artifacts:
      type: zip
      files:
        - template-export.yml
        - '**/*'

和我的template.yml:

    Resources:
      HelloWorld:
        Type: AWS::Serverless::Function
        Properties:
          Handler: index.handler
          Runtime: python3.6
          Environment:
            Variables:
              PYTHONPATH: ./lib
          Role:
            Fn::ImportValue:
              !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
          Events:
            GetEvent:
              Type: Api
              Properties:
                Path: /
                Method: get
            PostEvent:
              Type: Api
              Properties:
                Path: /
                Method: post

1 个答案:

答案 0 :(得分:1)

上述方法不起作用的原因是,无论出于何种原因,PYTHONPATH在AWS Lambda上都不起作用(即使它似乎可以与Codebuild一起使用)。以下配置有效。

buildspec.yml:

    version: 0.2

    phases:
      install:
        commands:
          - pip install -r requirements.txt -t .
          # Upgrade AWS CLI to the latest version
          - pip install --upgrade awscli

      pre_build:
        commands:
          - python -V
          - export HOME_DIR=`pwd`
          - mkdir $HOME_DIR/nltk_data/
          - export NLTK_DATA=$HOME_DIR/nltk_data
          - python -m nltk.downloader -d $NLTK_DATA punkt
          - python -m unittest discover tests

      build:
        commands:
          - aws cloudformation package --template template.yml --s3-bucket 
$S3_BUCKET --output-template template-export.yml

    artifacts:
      type: zip
      files:
        - template-export.yml
        - '**/*'

和我的template.yml:

    Resources:
      HelloWorld:
        Type: AWS::Serverless::Function
        Properties:
          Handler: index.handler
          Runtime: python3.6
          Environment:
            Variables:
              NLTK_DATA: ./nltk_data
          Role:
            Fn::ImportValue:
              !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
          Events:
            GetEvent:
              Type: Api
              Properties:
                Path: /
                Method: get
            PostEvent:
              Type: Api
              Properties:
                Path: /
                Method: post