首页 文章

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

提问于
浏览
1

我希望能够在某个地方的 requirements.txt 文件中指定它们,但这似乎并不那么简单 . 具体来说,我想部署一个依赖于nltk的lambda处理程序,并将nltk tokenizer "punkt"的文件作为Codebuild进程的一部分下载并打包为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 回答

  • 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
    

相关问题