使用 Python 平台 - AWS App Runner

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Python 平台

P AWS App Runner ython 平台提供托管运行时。每个运行时都可以轻松构建和运行基于 Python 版本的 Web 应用程序的容器。当你使用 Python 运行时时,App Runner 从托管的 Python 运行时镜像开始。此镜像基于 Amazon Linux Docker 镜像,包含某个 Python 版本的运行时包以及一些工具和常用的依赖包。App Runner 使用此托管运行时映像作为基础映像,并添加您的应用程序代码来构建 Docker 映像。然后,它会部署此映像以在容器中运行您的 Web 服务。

在使用 App Runner 控制台或 CreateServiceAPI 操作创建服务时,可以为 App Runner 服务指定运行时。您也可以将运行时指定为源代码的一部分。在包含在代码存储库中的 A pp Runner 配置文件中使用runtime关键字。托管运行时的命名约定是 <language-name><major-version>

有关有效的 Python 运行时名称和版本,请参见Python 运行时发布信息

每次部署或服务更新时,App Runner 都会将服务的运行时更新到最新版本。如果您的应用程序需要托管运行时的特定版本,则可以使用 App Runner 配置文件中的runtime-version关键字进行指定。您可以锁定到任何级别的版本,包括主要版本或次要版本。App Runner 仅对服务的运行时进行较低级别的更新。

Python 运行时的版本语法:major[.minor[.patch]]

例如:3.8.5

以下示例演示了版本锁定:

  • 3.8— 锁定主要版本和次要版本。App Runner 仅更新补丁版本。

  • 3.8.5— 锁定到特定的补丁版本。App Runner 不会更新你的运行时版本。

Python 运行时配置

选择托管运行时时,还必须至少配置生成和运行命令。您可以在创建更新 App Runner 服务时对其进行配置。您可以使用以下方法之一来执行此操作:

  • 使用 App Runner 控制台-在创建过程或配置选项卡的 “配置构建” 部分中指定命令。

  • 使用 App Runner API-调用CreateServiceUpdateServiceAPI 操作。使用CodeConfigurationValues数据类型的BuildCommandStartCommand成员来指定命令。

  • 使用配置文件-在最多三个构建阶段中指定一个或多个构建命令,并指定一个用于启动应用程序的运行命令。还有其他可选的配置设置。

提供配置文件是可选的。使用控制台或 API 创建 App Runner 服务时,您可以指定 App Runner 是在创建时直接获取配置设置还是从配置文件中获取配置设置。

特定运行时版本的标注

注意

App Runner 现在基于以下运行时版本为应用程序运行更新的构建流程:Python 3.11 和 Node.js 18。如果您的应用程序在其中一个运行时版本上运行,请参阅托管运行时版本和 App Runner 版本,了解有关修订后的构建过程的更多信息。使用所有其他运行时版本的应用程序不受影响,它们将继续使用原始的构建过程。

Python 3.11(修订的 App Runner 版本)

apprunner.yaml 中为托管 P ython 3.11 运行时使用以下设置。

  • 将 “顶部” 部分的runtime密钥设置为 python311

    runtime: python311
  • 使用代pip3pip来安装依赖关系。

  • 请改用python3解释器python

  • pip3安装程序作为pre-run命令运行。Python 在/app目录之外安装依赖项。由于 App Runner 运行适用于 Python 3.11 的修订版 App Runner 版本,因此通过apprunner.yaml文件的 “构建” 部分中的命令安装在/app目录之外的任何内容都将丢失。有关更多信息,请参阅 修订后的 App Runner 版本

    run: runtime-version: 3.11 pre-run: - pip3 install pipenv - pipenv install - python3 copy-global-files.py command: pipenv run gunicorn django_apprunner.wsgi --log-file -

有关更多信息,另请参阅本主题后面的 Python 3.11 扩展配置文件示例

Python 运行时示例

以下示例显示了用于构建和运行 Python 服务的 App Runner 配置文件。最后一个示例是可以部署到 Python 运行时服务的完整 Python 应用程序的源代码。

注意

这些示例中使用的运行时版本是 3.7.7 和 3. 11。您可以将其替换为要使用的版本。有关支持的最新 Python 运行时版本,请参阅Python 运行时发布信息

此示例显示了可以与 Python 托管运行时配合使用的最小配置文件。有关 App Runner 使用最小配置文件做出的假设,请参阅配置文件示例

Python 3.11 使用pip3python3命令。有关更多信息,请参阅本主题后面的 Python 3.11 扩展配置文件示例

例 apprunner.yaml
version: 1.0 runtime: python3 build: commands: build: - pip install pipenv - pipenv install run: command: python app.py

此示例展示了在 Python 托管运行时中使用所有配置密钥的情况。

注意

这些示例中使用的运行时版本为 3.7.7。您可以将其替换为要使用的版本。有关支持的最新 Python 运行时版本,请参阅Python 运行时发布信息

Python 3.11 使用pip3python3命令。有关更多信息,请参阅本主题后面的 Python 3.11 扩展配置文件示例。

例 apprunner.yaml
version: 1.0 runtime: python3 build: commands: pre-build: - wget -c https://s3.amazonaws.com/DOC-EXAMPLE-BUCKET/test-lib.tar.gz -O - | tar -xz build: - pip install pipenv - pipenv install post-build: - python manage.py test env: - name: DJANGO_SETTINGS_MODULE value: "django_apprunner.settings" - name: MY_VAR_EXAMPLE value: "example" run: runtime-version: 3.7.7 command: pipenv run gunicorn django_apprunner.wsgi --log-file - network: port: 8000 env: MY_APP_PORT env: - name: MY_VAR_EXAMPLE value: "example" secrets: - name: my-secret value-from: "arn:aws:secretsmanager:us-east-1:123456789012:secret:testingstackAppRunnerConstr-kJFXde2ULKbT-S7t8xR:username::" - name: my-parameter value-from: "arn:aws:ssm:us-east-1:123456789012:parameter/parameter-name" - name: my-parameter-only-name value-from: "parameter-name"

此示例显示了在 Python 3.11 托管运行时中使用所有配置密钥的情况。apprunner.yaml此示例包括一pre-run节,因为此版本的 Python 使用修订后的 App Runner 版本。

只有修订后的 App Runner 版本支持该pre-run参数。如果您的应用程序使用原始 App Runner 版本支持的运行时版本,请不要在配置文件中插入此参数。有关更多信息,请参阅 托管运行时版本和 App Runner 版本

注意

这些示例中使用的运行时版本是 3.11。您可以将其替换为要使用的版本。有关支持的最新 Python 运行时版本,请参阅Python 运行时发布信息

例 apprunner.yaml
version: 1.0 runtime: python311 build: commands: pre-build: - wget -c https://s3.amazonaws.com/DOC-EXAMPLE-BUCKET/test-lib.tar.gz -O - | tar -xz build: - pip3 install pipenv - pipenv install post-build: - python3 manage.py test env: - name: DJANGO_SETTINGS_MODULE value: "django_apprunner.settings" - name: MY_VAR_EXAMPLE value: "example" run: runtime-version: 3.11 pre-run: - pip3 install pipenv - pipenv install - python3 copy-global-files.py command: pipenv run gunicorn django_apprunner.wsgi --log-file - network: port: 8000 env: MY_APP_PORT env: - name: MY_VAR_EXAMPLE value: "example" secrets: - name: my-secret value-from: "arn:aws:secretsmanager:us-east-1:123456789012:secret:testingstackAppRunnerConstr-kJFXde2ULKbT-S7t8xR:username::" - name: my-parameter value-from: "arn:aws:ssm:us-east-1:123456789012:parameter/parameter-name" - name: my-parameter-only-name value-from: "parameter-name"

此示例显示了可以部署到 Python 运行时服务的完整 Python 应用程序的源代码。

例 requirements.txt
pyramid==2.0
例 server.py
from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Response import os def hello_world(request): name = os.environ.get('NAME') if name == None or len(name) == 0: name = "world" message = "Hello, " + name + "!\n" return Response(message) if __name__ == '__main__': port = int(os.environ.get("PORT")) with Configurator() as config: config.add_route('hello', '/') config.add_view(hello_world, route_name='hello') app = config.make_wsgi_app() server = make_server('0.0.0.0', port, app) server.serve_forever()
例 apprunner.yaml
version: 1.0 runtime: python3 build: commands: build: - pip install -r requirements.txt run: command: python server.py