使用Lambda在AWS Codestar中安装Python依赖项 [英] Installing Python dependencies in AWS Codestar with Lambda
问题描述
我正在尝试AWS Codestar.我的目标是部署一个非平凡的lambda端点,即处理程序具有依赖项的地方.理想情况下,我希望能够在requirements.txt
文件中的某个位置指定它们,但这似乎不是那么简单.具体来说,我想部署一个依赖于nltk的lambda处理程序,并在Codebuild流程的一部分中下载nltk标记程序"punkt"的文件,并将其打包为Lambda.
I'm trying out AWS Codestar. My objective is to deploy a non-trivial lambda endpoint, that is, where the handler has dependencies. Ideally, I'd like to be able to specify them in a requirements.txt
file somewhere but this seems not to be so straightforward. Specifically, I would like to deploy a lambda handler that depends on nltk and where the files for the nltk tokenizer "punkt" are downloaded as part of the Codebuild process and packaged up for Lambda.
如何通过buildspec.yml
和template.yml
完成此操作?在下面,我试图将pip
依赖项安装到子目录lib
并将其包含在zip工件中.
How can this be done through buildspec.yml
and template.yml
? Below, I'm trying to install the pip
dependencies to a subdirectory lib
and include this in the zip artifact.
运行时,Codebuild能够安装依赖项,导入nltk并运行测试,成功部署到Lambda,并且正确的文件被打包在lib
子文件夹中(我下载了ZIP文件进行检查),但是我查看Lambda日志中的错误:unable to import module 'index': No module named 'nltk'
.
When run, Codebuild is able to install dependencies, import nltk and run tests, the deployment to Lambda succeeds, and the right files are being packaged up in the lib
subfolder (I downloaded the ZIP file to check) but I see errors in the Lambda logs: 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:
and my 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
推荐答案
上面的方法不起作用的原因是,无论出于何种原因,PYTHONPATH
都不适用于AWS Lambda(即使它似乎可以与Codebuild一起使用) ).以下配置有效.
The reason the above didn't work is that for whatever reason, PYTHONPATH
doesn't work on AWS Lambda (even though it seems to work with Codebuild). The below configuration works.
buildspec.yml:
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:
and my 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
这篇关于使用Lambda在AWS Codestar中安装Python依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!