使用容器镜像部署 Python Lambda 函数
有三种方法可以为 Python Lambda 函数构建容器映像:
-
Amazon 基本映像会预加载一个语言运行时系统、一个用于管理 Lambda 和函数代码之间交互的运行时系统接口客户端,以及一个用于本地测试的运行时系统接口仿真器。
-
Amazon 提供的基本映像
包含 Amazon Linux 或 Amazon Linux 2 操作系统以及运行时系统接口仿真器。您可以将首选的运行时系统、依赖项和代码添加到这些映像中。要使映像与 Lambda 兼容,您必须在映像中包含 Python 的运行时系统接口客户端。 -
您还可以使用其他容器注册表的备用基本映像,例如 Alpine Linux 或 Debian。您还可以使用您的组织创建的自定义映像。要使映像与 Lambda 兼容,您必须在映像中包含 Python 的运行时系统接口客户端。
提示
要缩短 Lambda 容器函数激活所需的时间,请参阅 Docker 文档中的使用多阶段构建
此页面介绍了如何为 Lambda 构建、测试和部署容器映像。
Python Amazon 基本映像
Amazon为 Python 提供了以下基本映像:
标签 | 运行时 | 操作系统 | Dockerfile | 淘汰 |
---|---|---|---|---|
3.11 |
Python 3.11 | Amazon Linux 2 | GitHub 上适用于 Python 3.11 的 Dockerfile |
|
3.10 |
Python 3.10 | Amazon Linux 2 | GitHub 上适用于 Python 3.10 的 Dockerfile |
|
3.9 |
Python 3.9 | Amazon Linux 2 | GitHub 上的适用于 Python 3.9 的 Dockerfile |
|
3.8 |
Python 3.8 | Amazon Linux 2 | GitHub 上的适用于 Python 3.8 的 Dockerfile |
|
3.7 |
Python 3.7 | Amazon Linux | GitHub 上的 适用于 Python 3.7 的 Dockerfile |
2023 年 11 月 27 日 |
Amazon ECR 存储库:gallery.ecr.aws/lambda/python
基本映像中的依赖项搜索路径
在代码中使用 import
语句时,Python 运行时系统会搜索其搜索路径中的目录,直到找到相应模块或包。默认情况下,运行时系统会首先搜索 {LAMBDA_TASK_ROOT}
目录。如果映像含有包含运行时系统的库的某个版本,则此版本将优先于运行时系统中包含的版本。
搜索路径中的其他步骤取决于您使用的 Python Lambda 基本映像的版本:
-
Python 3.11:包含运行时系统的库和安装了 pip 的库均安装在
/var/lang/lib/python3.11/site-packages
目录中。此目录优先于搜索路径中的/var/runtime
。您可以通过使用 pip 安装更新版本来覆盖 SDK。您可以使用 pip 来验证包含运行时系统的 SDK 及其依赖项是否与您安装的任何程序包兼容。 -
Python 3.7-3.10:包含运行时系统的库安装在
/var/runtime
目录中。安装了 pip 的库安装在/var/lang/lib/python3.x/site-packages
目录中。此/var/runtime
目录优先于搜索路径中的/var/lang/lib/python3.x/site-packages
。
通过添加以下代码段,您可以查看 Lambda 函数的完整搜索路径。
import sys search_path = sys.path print(search_path)
使用 Python 的 Amazon 基本映像
要完成本节中的步骤,您必须满足以下条件:
要从 Python 的 Amazon 的基本映像创建容器映像
-
为项目创建一个目录,然后切换到该目录。
mkdir example cd example
-
创建名为
lambda_function.py
的新文件。您可以将以下示例函数代码添加到文件中进行测试,也可以使用您自己的函数代码。例 Python 函数
import sys def handler(event, context): return 'Hello from Amazon Lambda using Python' + sys.version + '!'
-
创建名为
requirements.txt
的新文件。如果您使用的是上一步中的示例函数代码,则可以将文件留空,因为没有依赖项。否则,请列出每个必需的库。例如,如果您的函数使用的是 Amazon SDK for Python (Boto3),则requirements.txt
应如下所示:例 requirements.txt
boto3
-
使用以下配置创建一个新的 Dockerfile。
-
将
FROM
属性设置为基本映像的 URI。 -
使用 COPY 命令将函数代码和运行时系统依赖项复制到
{LAMBDA_TASK_ROOT}
。 -
将
CMD
参数设置为 Lambda 函数处理程序。
例 Dockerfile
FROM public.ecr.aws/lambda/python:3.11 # Copy requirements.txt COPY requirements.txt ${LAMBDA_TASK_ROOT} # Install the specified packages RUN pip install -r requirements.txt # Copy function code COPY lambda_function.py ${LAMBDA_TASK_ROOT} # Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile) CMD [ "lambda_function.handler" ]
-
-
使用 docker build
命令构建 Docker 映像。以下示例将映像命名为 docker-image
并为其提供test
标签。 docker build --platform linux/amd64 -t
docker-image
:test
.注意
该命令指定了
--platform linux/amd64
选项,可确保无论生成计算机的架构如何,容器始终与 Lambda 执行环境兼容。如果打算使用 ARM64 指令集架构创建 Lambda 函数,请务必将命令更改为使用--platform linux/arm64
选项。
-
使用 docker run 命令启动 Docker 映像。在此示例中,
docker-image
是映像名称,test
是标签。docker run -p 9000:8080
docker-image
:test
此命令会将映像作为容器运行,并在
localhost:9000/2015-03-31/functions/function/invocations
创建本地端点。 -
在新的终端窗口中,将事件发布到本地端点。
-
获取容器 ID。
docker ps
-
使用 docker kill
命令停止容器。在此命令中,将 3766c4ab331c
替换为上一步中的容器 ID。docker kill
3766c4ab331c
将映像上传到 Amazon ECR 并创建 Lambda 函数
-
运行 get-login-password
命令,以针对 Amazon ECR 注册表进行 Docker CLI 身份验证。 -
将
--region
值设置为要在其中创建 Amazon ECR 存储库的 Amazon Web Services 区域。 -
将
111122223333
替换为您的 Amazon Web Services 账户 ID。
aws ecr get-login-password --region
cn-north-1
| docker login --username AWS --password-stdin111122223333
.dkr.ecr.cn-north-1
.amazonaws.com.cn -
-
使用 create-repository
命令在 Amazon ECR 中创建存储库。 aws ecr create-repository --repository-name
hello-world
--regioncn-north-1
--image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE注意
Amazon ECR 存储库必须与 Lambda 函数位于同一 Amazon Web Services 区域 内。
如果成功,您将会看到如下响应:
{ "repository": { "repositoryArn": "arn:aws:ecr:cn-north-1:111122223333:repository/hello-world", "registryId": "111122223333", "repositoryName": "hello-world", "repositoryUri": "111122223333.dkr.ecr.cn-north-1.amazonaws.com.cn/hello-world", "createdAt": "2023-03-09T10:39:01+00:00", "imageTagMutability": "MUTABLE", "imageScanningConfiguration": { "scanOnPush": true }, "encryptionConfiguration": { "encryptionType": "AES256" } } }
-
从上一步的输出中复制
repositoryUri
。 -
运行 docker tag
命令,将本地映像作为最新版本标记到 Amazon ECR 存储库中。在此命令中: -
将
docker-image:test
替换为 Docker 映像的名称和标签。 -
将
<ECRrepositoryUri>
替换为复制的repositoryUri
。确保 URI 末尾包含:latest
。
docker tag docker-image:test <ECRrepositoryUri>:latest
示例:
docker tag
docker-image
:test
111122223333
.dkr.ecr.cn-north-1
.amazonaws.com.cn/hello-world
:latest -
-
运行 docker push
命令,以将本地映像部署到 Amazon ECR 存储库。确保存储库 URI 末尾包含 :latest
。docker push
111122223333
.dkr.ecr.cn-north-1
.amazonaws.com.cn/hello-world
:latest -
如果您还没有函数的执行角色,请创建执行角色。在下一步中,您需要提供角色的 Amazon 资源名称(ARN)。
-
创建 Lambda 函数。对于
ImageUri
,指定之前的存储库 URI。确保 URI 末尾包含:latest
。aws lambda create-function \ --function-name
hello-world
\ --package-type Image \ --code ImageUri=111122223333
.dkr.ecr.cn-north-1
.amazonaws.com.cn/hello-world
:latest \ --rolearn:aws:iam::111122223333:role/lambda-ex
注意
只要映像与 Lambda 函数位于同一区域内,您就可以使用其他 Amazon 账户中的映像创建函数。有关更多信息,请参阅 Amazon ECR 跨账户权限。
-
调用函数。
aws lambda invoke --function-name
hello-world
response.json应出现如下响应:
{ "ExecutedVersion": "$LATEST", "StatusCode": 200 }
-
要查看函数的输出,请检查
response.json
文件。
要更新函数代码,您必须再次构建映像,将新映像上传到 Amazon ECR 存储库,然后使用 update-function-code
将备用基本映像与运行时系统接口客户端配合使用
If you use a 自定义运行时系统的基本映像 or an alternative base image, you must include the runtime interface client in your image. The runtime interface client extends the Lambda 运行时 API, which manages the interaction between Lambda and your function code.使用 pip 程序包管理器安装 Python 的运行时系统接口客户端
pip install awslambdaric
您还可以从 GitHub 下载 Python 运行时接口客户端
以下示例演示了如何使用非 Amazon 基本映像为 Python 构建容器映像。示例 Dockerfile 使用官方 Python 基本映像。Dockerfile 包含 Python 的运行时系统接口客户端。
要完成本节中的步骤,您必须满足以下条件:
要从非 Amazon 基本映像创建容器映像
-
为项目创建一个目录,然后切换到该目录。
mkdir example cd example
-
创建名为
lambda_function.py
的新文件。您可以将以下示例函数代码添加到文件中进行测试,也可以使用您自己的函数代码。例 Python 函数
import sys def handler(event, context): return 'Hello from Amazon Lambda using Python' + sys.version + '!'
-
创建名为
requirements.txt
的新文件。如果您使用的是上一步中的示例函数代码,则可以将文件留空,因为没有依赖项。否则,请列出每个必需的库。例如,如果您的函数使用的是 Amazon SDK for Python (Boto3),则requirements.txt
应如下所示:例 requirements.txt
boto3
-
创建新 Dockerfile。以下 Dockerfile 使用官方 Python 基本映像而不是 Amazon 基本映像。Dockerfile 包含运行时系统接口客户端
,该客户端可使映像与 Lambda 兼容。以下示例 Dockerfile 将使用多阶段构建 。 -
将
FROM
属性设置为基本映像。 -
将
ENTRYPOINT
设置为您希望 Docker 容器在启动时运行的模块。在本例中,模块为运行时系统接口客户端。 -
将
CMD
设置为 Lambda 函数处理程序。
例 Dockerfile
# Define custom function directory ARG FUNCTION_DIR="/function" FROM
python:3.11
as build-image # Include global arg in this stage of the build ARG FUNCTION_DIR # Copy function code RUN mkdir -p ${FUNCTION_DIR} COPY . ${FUNCTION_DIR} # Install the function's dependencies RUN pip install \ --target ${FUNCTION_DIR} \ awslambdaric # Use a slim version of the base Python image to reduce the final image size FROMpython:3.11-slim
# Include global arg in this stage of the build ARG FUNCTION_DIR # Set working directory to function root directory WORKDIR ${FUNCTION_DIR} # Copy in the built dependencies COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR} # Set runtime interface client as default command for the container runtime ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric
" ] # Pass the name of the function handler as an argument to the runtime CMD [ "lambda_function.handler
" ] -
-
使用 docker build
命令构建 Docker 映像。以下示例将映像命名为 docker-image
并为其提供test
标签。 docker build --platform linux/amd64 -t
docker-image
:test
.注意
该命令指定了
--platform linux/amd64
选项,可确保无论生成计算机的架构如何,容器始终与 Lambda 执行环境兼容。如果打算使用 ARM64 指令集架构创建 Lambda 函数,请务必将命令更改为使用--platform linux/arm64
选项。
使用运行时系统接口仿真器在本地测试映像。您可以将仿真器构建到映像中,也可以将其安装在本地计算机上。
在本地计算机上安装并运行运行时系统接口仿真器
-
从项目目录中,运行以下命令以从 GitHub 下载运行时系统接口仿真器(x86-64 架构)并将其安装在本地计算机上。
-
使用 docker run 命令启动 Docker 映像。请注意以下几点:
-
docker-image
是映像名称,test
是标签。 -
/usr/local/bin/python -m awslambdaric lambda_function.handler
是ENTRYPOINT
,后跟您 Dockerfile 中的CMD
。
此命令会将映像作为容器运行,并在
localhost:9000/2015-03-31/functions/function/invocations
创建本地端点。 -
-
将事件发布到本地端点。
-
获取容器 ID。
docker ps
-
使用 docker kill
命令停止容器。在此命令中,将 3766c4ab331c
替换为上一步中的容器 ID。docker kill
3766c4ab331c
将映像上传到 Amazon ECR 并创建 Lambda 函数
-
运行 get-login-password
命令,以针对 Amazon ECR 注册表进行 Docker CLI 身份验证。 -
将
--region
值设置为要在其中创建 Amazon ECR 存储库的 Amazon Web Services 区域。 -
将
111122223333
替换为您的 Amazon Web Services 账户 ID。
aws ecr get-login-password --region
cn-north-1
| docker login --username AWS --password-stdin111122223333
.dkr.ecr.cn-north-1
.amazonaws.com.cn -
-
使用 create-repository
命令在 Amazon ECR 中创建存储库。 aws ecr create-repository --repository-name
hello-world
--regioncn-north-1
--image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE注意
Amazon ECR 存储库必须与 Lambda 函数位于同一 Amazon Web Services 区域 内。
如果成功,您将会看到如下响应:
{ "repository": { "repositoryArn": "arn:aws:ecr:cn-north-1:111122223333:repository/hello-world", "registryId": "111122223333", "repositoryName": "hello-world", "repositoryUri": "111122223333.dkr.ecr.cn-north-1.amazonaws.com.cn/hello-world", "createdAt": "2023-03-09T10:39:01+00:00", "imageTagMutability": "MUTABLE", "imageScanningConfiguration": { "scanOnPush": true }, "encryptionConfiguration": { "encryptionType": "AES256" } } }
-
从上一步的输出中复制
repositoryUri
。 -
运行 docker tag
命令,将本地映像作为最新版本标记到 Amazon ECR 存储库中。在此命令中: -
将
docker-image:test
替换为 Docker 映像的名称和标签。 -
将
<ECRrepositoryUri>
替换为复制的repositoryUri
。确保 URI 末尾包含:latest
。
docker tag docker-image:test <ECRrepositoryUri>:latest
示例:
docker tag
docker-image
:test
111122223333
.dkr.ecr.cn-north-1
.amazonaws.com.cn/hello-world
:latest -
-
运行 docker push
命令,以将本地映像部署到 Amazon ECR 存储库。确保存储库 URI 末尾包含 :latest
。docker push
111122223333
.dkr.ecr.cn-north-1
.amazonaws.com.cn/hello-world
:latest -
如果您还没有函数的执行角色,请创建执行角色。在下一步中,您需要提供角色的 Amazon 资源名称(ARN)。
-
创建 Lambda 函数。对于
ImageUri
,指定之前的存储库 URI。确保 URI 末尾包含:latest
。aws lambda create-function \ --function-name
hello-world
\ --package-type Image \ --code ImageUri=111122223333
.dkr.ecr.cn-north-1
.amazonaws.com.cn/hello-world
:latest \ --rolearn:aws:iam::111122223333:role/lambda-ex
注意
只要映像与 Lambda 函数位于同一区域内,您就可以使用其他 Amazon 账户中的映像创建函数。有关更多信息,请参阅 Amazon ECR 跨账户权限。
-
调用函数。
aws lambda invoke --function-name
hello-world
response.json应出现如下响应:
{ "ExecutedVersion": "$LATEST", "StatusCode": 200 }
-
要查看函数的输出,请检查
response.json
文件。
要更新函数代码,您必须再次构建映像,将新映像上传到 Amazon ECR 存储库,然后使用 update-function-code
对于如何从 Alpine 基本镜像创建 Python 镜像的示例,请参阅Amazon博客上的 Lambda 容器镜像支持