AWS Lambda
开发人员指南

Python 中的 AWS Lambda 部署程序包

A deployment package is a ZIP archive that contains your function code and dependencies. You need to create a deployment package if you use the Lambda API to manage functions, or if your code uses libraries other than the AWS SDK. Other libraries and dependencies need to be included in the deployment package. You can upload the package directly to Lambda, or you can use an Amazon S3 bucket, and then upload it to Lambda.

If you use the Lambda console editor to author your function, the console manages the deployment package. You can use this method as long as you don't need to add any libraries. You can also use it to update a function that already has libraries in the deployment package, as long as the total size doesn't exceed 3 MB.

注意

您可以使用 AWS SAM CLI build 命令为 Python 函数代码和依赖项创建部署包。有关说明,请参阅《AWS SAM 开发人员指南》中的构建具有依赖项的应用程序

没有附加依赖项

要使用 Lambda API 创建或更新函数,请创建包含函数代码的存档,并使用 AWS CLI 上传。

更新没有依赖项的 Python 函数

  1. 创建 ZIP 存档。

    ~/my-function$ zip function.zip function.py
  2. 使用 update-function-code 命令上传程序包。

    ~/my-function$ aws lambda update-function-code --function-name python37 --zip-file fileb://function.zip { "FunctionName": "python37", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:python37", "Runtime": "python3.7", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "function.handler", "CodeSize": 815, "Description": "", "Timeout": 3, "MemorySize": 128, "LastModified": "2018-11-20T20:41:16.647+0000", "CodeSha256": "GcZ05oeHoJi61VpQj7vCLPs8DwCXmX5sE/fE2IHsizc=", "Version": "$LATEST", "VpcConfig": { "SubnetIds": [], "SecurityGroupIds": [], "VpcId": "" }, "TracingConfig": { "Mode": "Active" }, "RevisionId": "d1e983e3-ca8e-434b-8dc1-7add83d72ebd" }

具有附加依赖项

如果您的函数依赖于 适用于 Python 的 开发工具包 (Boto3) 之外的库,请使用 pip 将它们安装到本地目录中,并将它们包含在部署包程序中。

更新有依赖项的 Python 函数

  1. 为依赖项创建一个目录。

    ~/my-function$ mkdir package
  2. 使用 --target 选项安装程序包目录中的库。

    ~/my-function$ cd package ~/my-function/package$ pip install Pillow --target . Collecting Pillow Using cached https://files.pythonhosted.org/packages/62/8c/230204b8e968f6db00c765624f51cfd1ecb6aea57b25ba00b240ee3fb0bd/Pillow-5.3.0-cp37-cp37m-manylinux1_x86_64.whl Installing collected packages: Pillow Successfully installed Pillow-5.3.0
  3. 创建 ZIP 存档。

    package$ zip -r9 ../function.zip . adding: PIL/ (stored 0%) adding: PIL/.libs/ (stored 0%) adding: PIL/.libs/libfreetype-7ce95de6.so.6.16.1 (deflated 65%) adding: PIL/.libs/libjpeg-3fe7dfc0.so.9.3.0 (deflated 72%) adding: PIL/.libs/liblcms2-a6801db4.so.2.0.8 (deflated 67%) ...
  4. 将您的函数代码添加到存档中。

    ~/my-function/package$ cd ../ ~/my-function$ zip -g function.zip function.py adding: function.py (deflated 56%)
  5. 更新函数代码。

    ~/my-function$ aws lambda update-function-code --function-name python37 --zip-file fileb://function.zip { "FunctionName": "python37", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:python37", "Runtime": "python3.7", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "function.handler", "CodeSize": 2269409, "Description": "", "Timeout": 3, "MemorySize": 128, "LastModified": "2018-11-20T20:51:35.871+0000", "CodeSha256": "GcZ05oeHoJi61VpQj7vCLPs8DwCXmX5sE/fE2IHsizc=", "Version": "$LATEST", "VpcConfig": { "SubnetIds": [], "SecurityGroupIds": [], "VpcId": "" }, "TracingConfig": { "Mode": "Active" }, "RevisionId": "a9c05ffd-8ad6-4d22-b6cd-d34a00c1702c" }

使用虚拟环境

有些情况下,您可能需要使用虚拟环境来安装函数的依赖项。如果您的函数或其依赖项依赖于本机库,或者您使用 Homebrew 安装 Python,则会发生这种情况。

使用虚拟环境更新 Python 函数

  1. 创建虚拟环境。

    ~/my-function$ virtualenv v-env Using base prefix '~/.local/python-3.7.0' New python executable in v-env/bin/python3.7 Also creating executable in v-env/bin/python Installing setuptools, pip, wheel... done.
  2. 激活环境。

    ~/my-function$ source v-env/bin/activate (v-env) ~/my-function$

    对于 Windows 命令行,激活脚本位于 Scripts 目录中。

    > v-env\Scripts\activate.bat
  3. 使用 pip 安装库。

    ~/my-function$ pip install Pillow Collecting Pillow Using cached https://files.pythonhosted.org/packages/62/8c/230204b8e968f6db00c765624f51cfd1ecb6aea57b25ba00b240ee3fb0bd/Pillow-5.3.0-cp37-cp37m-manylinux1_x86_64.whl Installing collected packages: Pillow Successfully installed Pillow-5.3.0
  4. 停用虚拟环境。

    (v-env)~/my-function$ deactivate
  5. 使用 site-packages 目录的内容创建 ZIP 存档。

    ~/my-function$ cd v-env/lib/python3.7/site-packages/ ~/my-function/v-env/lib/python3.7/site-packages$ zip -r9 ../../../../function.zip . adding: easy_install.py (deflated 17%) adding: PIL/ (stored 0%) adding: PIL/.libs/ (stored 0%) adding: PIL/.libs/libfreetype-7ce95de6.so.6.16.1 (deflated 65%) adding: PIL/.libs/libjpeg-3fe7dfc0.so.9.3.0 (deflated 72%) ...

    注意

    有些情况下,库还可能安装在 dist-packages 目录中。

  6. 将您的函数代码添加到存档中。

    ~/my-function/v-env/lib/python3.7/site-packages$ cd ../../../../ ~/my-function$ zip -g function.zip function.py adding: function.py (deflated 56%)
  7. 更新函数代码。

    ~/my-function$ aws lambda update-function-code --function-name python37 --zip-file fileb://function.zip { "FunctionName": "python37", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:python37", "Runtime": "python3.7", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "function.handler", "CodeSize": 5912988, "Description": "", "Timeout": 3, "MemorySize": 128, "LastModified": "2018-11-20T21:08:26.326+0000", "CodeSha256": "A2P0NUWq1J+LtSbkuP8tm9uNYqs1TAa3M76ptmZCw5g=", "Version": "$LATEST", "VpcConfig": { "SubnetIds": [], "SecurityGroupIds": [], "VpcId": "" }, "TracingConfig": { "Mode": "Active" }, "RevisionId": "5afdc7dc-2fcb-4ca8-8f24-947939ca707f" }