使用输入构建 Python Lambda 函数 uv AWS SAM - AWS Serverless Application Model

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

使用输入构建 Python Lambda 函数 uv AWS SAM

此功能为预览版 AWS SAM ,可能会发生变化。

使用带有uv快速 Python 包安装程序和解析器的 AWS Serverless Application Model 命令行界面 (AWS SAMCLI) 来构建 Python AWS Lambda 函数。

先决条件

Python

要安装 Python,请参阅在 Python 网站上下载 Python

uv

AWS SAMCLI需要安装速度极快的 uvPython 包安装程序和解析器。有关安装说明,请参阅 uv 文档中的安装

选择加入 AWS SAM CLI 测试版功能

由于此功能为预览版,因此您必须使用以下方法之一选择加入:

  1. 使用环境变量:SAM_CLI_BETA_PYTHON_UV=1

  2. 在您的 samconfig.toml 文件中添加以下内容:

    [default.build.parameters] beta_features = true [default.sync.parameters] beta_features = true
  3. 使用支持的 AWS SAM CLI 命令时使用 --beta-features 选项。例如:

    $ sam build --beta-features
  4. 当 AWS SAM CLI 提示您选择加入时,请选择选项 y。以下是示例:

    $ sam build Starting Build use cache Build method "python-uv" is a beta feature. Please confirm if you would like to proceed You can also enable this beta feature with "sam build --beta-features". [y/N]: y

配置 AWS SAM 为与 Python Lambda 函数一起使用以及 uv

第 1 步:配置您的 AWS SAM 模板

使用以下内容配置您的 AWS SAM 模板:

  • BuildMethodpython-uv.

  • CodeUri— 包含pyproject.toml或的函数代码目录的路径requirements.txt

  • H andler — 你的函数处理器(例如app.lambda_handler)。

  • 运行时 — Python 运行时版本(例如python3.12)。

以下是已配置 AWS SAM 模板的示例:

AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 ... Resources: MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./my_function Handler: app.lambda_handler Runtime: python3.12 Metadata: BuildMethod: python-uv ...

示例

Hello World 示例

在此示例中,我们使用 Python uv 作为包管理器构建了一个示例 Hello World 应用程序。

uv可以使用pyproject.toml或来读requirements.txt取依赖关系。如果两者都给出,sam build将从中读requirements.txt取依赖关系。

以下是 Hello World 应用程序的结构:

hello-python-uv
├── README.md
├── events
│   └── event.json
├── hello_world
│   ├── __init__.py
│   ├── app.py
│   └── pyproject.toml
├── samconfig.toml
└── template.yaml

pyproject.toml 文件:

[project] name = "my-function" version = "0.1.0" requires-python = ">=3.12" dependencies = [ "requests>=2.31.0", "boto3>=1.28.0", ]

在我们的 AWS SAM 模板中,我们的 Python 函数定义如下:

AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 ... Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: hello_world/ Handler: app.lambda_handler Runtime: python3.12 Architectures: - x86_64 Metadata: BuildMethod: python-uv

接下来,运行 sam build 以构建应用程序并准备部署。 AWS SAM CLI 创建一个 .aws-sam 目录并在其中整理构建构件。我们的函数依赖项是使用安装uv并存储在的.aws-sam/build/HelloWorldFunction/

hello-python-uv$ sam build Starting Build use cache Build method "python-uv" is a beta feature. Please confirm if you would like to proceed You can also enable this beta feature with "sam build --beta-features". [y/N]: y Experimental features are enabled for this session. Visit the docs page to learn more about the AWS Beta terms https://aws.amazon.com/service-terms/. Cache is invalid, running build and copying resources for following functions (HelloWorldFunction) Building codeuri: /Users/.../hello-python-uv/hello_world runtime: python3.12 metadata: {'BuildMethod': 'python-uv'} architecture: x86_64 functions: HelloWorldFunction Running PythonUvBuilder:UvBuild Running PythonUvBuilder:CopySource Build Succeeded Built Artifacts : .aws-sam/build Built Template : .aws-sam/build/template.yaml Commands you can use next ========================= [*] Validate SAM template: sam validate [*] Invoke Function: sam local invoke [*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch [*] Deploy: sam deploy --guided
注意

python-uv构建方法是按Metadata部分中的每个函数配置的。模板中的每个函数都可以使用不同的构建方法,从而允许您在同一个 AWS SAM 模板中将uv基于的函数与pip基于的函数混合使用。如果未指定构建方法,pip则默认使用。