我从AWS codebuild上传了我的lambda函数源。我的Python脚本使用NLTK,因此它需要大量数据。我的.zip包太大了,发生了RequestEntityTooLargeException
。我想知道如何增加通过UpdateFunctionCode命令发送的部署包的大小。
我使用AWS CodeBuild
将源从GitHub存储库转换为AWS Lambda
。这是关联的buildspec文件:
version: 0.2
phases:
install:
commands:
- echo "install step"
- apt-get update
- apt-get install zip -y
- apt-get install python3-pip -y
- pip install --upgrade pip
- pip install --upgrade awscli
# Define directories
- export HOME_DIR=`pwd`
- export NLTK_DATA=$HOME_DIR/nltk_data
pre_build:
commands:
- echo "pre_build step"
- cd $HOME_DIR
- virtualenv venv
- . venv/bin/activate
# Install modules
- pip install -U requests
# NLTK download
- pip install -U nltk
- python -m nltk.downloader -d $NLTK_DATA wordnet stopwords punkt
- pip freeze > requirements.txt
build:
commands:
- echo 'build step'
- cd $HOME_DIR
- mv $VIRTUAL_ENV/lib/python3.6/site-packages/* .
- sudo zip -r9 algo.zip .
- aws s3 cp --recursive --acl public-read ./ s3://hilightalgo/
- aws lambda update-function-code --function-name arn:aws:lambda:eu-west-3:671560023774:function:LaunchHilight --zip-file fileb://algo.zip
- aws lambda update-function-configuration --function-name arn:aws:lambda:eu-west-3:671560023774:function:LaunchHilight --environment 'Variables={NLTK_DATA=/var/task/nltk_data}'
post_build:
commands:
- echo "post_build step"
当我启动管道时,我有RequestEntityTooLargeException
,因为我的.zip包中有太多数据。请参阅下面的构建日志:
[Container] 2019/02/11 10:48:35 Running command aws lambda update-function-code --function-name arn:aws:lambda:eu-west-3:671560023774:function:LaunchHilight --zip-file fileb://algo.zip
An error occurred (RequestEntityTooLargeException) when calling the UpdateFunctionCode operation: Request must be smaller than 69905067 bytes for the UpdateFunctionCode operation
[Container] 2019/02/11 10:48:37 Command did not exit successfully aws lambda update-function-code --function-name arn:aws:lambda:eu-west-3:671560023774:function:LaunchHilight --zip-file fileb://algo.zip exit status 255
[Container] 2019/02/11 10:48:37 Phase complete: BUILD Success: false
[Container] 2019/02/11 10:48:37 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: aws lambda update-function-code --function-name arn:aws:lambda:eu-west-3:671560023774:function:LaunchHilight --zip-file fileb://algo.zip. Reason: exit status 255
当我减少要下载的NLTK数据时,一切都正常工作(我只尝试了包stopwords
和wordnet
。
有没有人有想法解决这个“尺寸限制问题”?
您无法增加Lambda的部署包大小。 AWS Lambda限制在AWS Lambda devopler guide中描述。有关这些限制如何工作的更多信息可以看作here。实质上,您的解压缩包大小必须小于250MB(262144000字节)。
PS:使用图层并不能解决尺寸问题,但有助于管理和更快的冷启动。包装尺寸包括层 - Lambda layers。
一个功能一次最多可以使用5个图层。函数和所有图层的总解压缩大小不能超过250 MB的解压缩部署包大小限制。
我自己没有尝试过,但Zappa的人们描述了一个可能有用的技巧。引自https://blog.zappa.io/posts/slim-handler:
Zappa压缩大型应用程序并将项目zip文件发送到S3。其次,Zappa创建了一个非常小的slim处理程序,它只包含Zappa及其依赖项,并将其发送给Lambda。
当在冷启动时调用slim处理程序时,它会从S3下载大型项目zip并将其解压缩到Lambda的shared / tmp空间中。对该温暖Lambda的所有后续调用共享/ tmp空间并可访问项目文件;所以如果Lambda保持温暖,文件可能只下载一次。
这样你应该得到500MB / tmp。
您无法增加包大小,但可以使用AWS Lambda层来存储某些应用程序依赖项。
https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path
在此层之前,一个常用的模式来解决此限制是从S3下载巨大的依赖项。