

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# uv で を使用して Python Lambda 関数を構築する AWS SAM
<a name="building-python-uv"></a>


|  | 
| --- |
| この機能は のプレビューリリースであり AWS SAM 、変更される可能性があります。 | 

高速 Python パッケージインストーラおよびリゾルバーuvである で AWS Serverless Application Model コマンドラインインターフェイス (AWS SAM CLI) を使用して、Python AWS Lambda 関数を構築します。

**Topics**
+ [前提条件](#building-python-uv-prerequisites)
+ [Python Lambda 関数と で使用する AWS SAM ように を設定する uv](#building-python-uv-configure)
+ [例](#building-python-uv-examples)

## 前提条件
<a name="building-python-uv-prerequisites"></a>

**Python**  
Python をインストールするには、[Python ウェブサイトの「Python のダウンロード](https://www.python.org/downloads/)」を参照してください。 **

**uv**  
には[https://docs.astral.sh/uv/](https://docs.astral.sh/uv/)、非常に高速な Python パッケージインストーラおよびリゾルバーである のインストール AWS SAM CLIが必要です。インストール手順については、「*uv ドキュメント*」で「[Installation](https://docs.astral.sh/uv/getting-started/installation/)」を参照してください。

** AWS SAM CLI ベータ機能にオプトインする**  
この機能はプレビュー段階にあるため、次のいずれかの方法を使用してオプトインする必要があります。  

1. 次の環境変数を使用します: `SAM_CLI_BETA_PYTHON_UV=1`。

1. 次のコードを `samconfig.toml` ファイルに追加します。

   ```
   [default.build.parameters]
   beta_features = true
   [default.sync.parameters]
   beta_features = true
   ```

1. サポートされている AWS SAM CLI のコマンドを使用する場合は、`--beta-features` オプションを使用します。例えば、次のようになります。

   ```
   $ sam build --beta-features
   ```

1.  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
   ```

## Python Lambda 関数と で使用する AWS SAM ように を設定する uv
<a name="building-python-uv-configure"></a>

### ステップ 1: AWS SAM テンプレートを設定する
<a name="building-python-uv-configure-template"></a>

以下を使用して AWS SAM テンプレートを設定します。
+ **BuildMethod** – `python-uv`。
+ **CodeUri** – `pyproject.toml`または を含む関数コードディレクトリへのパス`requirements.txt`。
+ **ハンドラー** – 関数ハンドラー (例: `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
...
```

## 例
<a name="building-python-uv-examples"></a>

### Hello World の例
<a name="building-python-uv-examples-hello"></a>

**この例では、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`セクションの関数ごとに設定されます。テンプレート内の各関数は異なるビルドメソッドを使用できるため、 uvベースの関数を同じ AWS SAM テンプレート内の `pip`ベースの関数と混在させることができます。ビルドメソッドが指定されていない場合は、デフォルトで `pip` が使用されます。