uv에서를 사용하여 Python Lambda 함수 빌드 AWS SAM - AWS Serverless Application Model

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

uv에서를 사용하여 Python Lambda 함수 빌드 AWS SAM

이 기능은에 대한 평가판 릴리스 AWS SAM 이며 변경될 수 있습니다.

빠른 Python 패키지 설치 관리자 및 해석기uv인와 함께 AWS Serverless Application Model 명령줄 인터페이스(AWS SAM CLI)를 사용하여 Python AWS Lambda 함수를 빌드합니다.

사전 조건

Python

Python을 설치하려면 Python 웹 사이트의 Python 다운로드를 참조하세요.

uv

에는 AWS SAM CLI 매우 빠른 Python 패키지 설치 관리자 및 해석기uv인를 설치해야 합니다. 설치에 대한 지침은 uv 설명서설치 섹션을 참조하세요.

AWS SAMCLI 베타 기능에 대한 옵트인

이 기능은 미리 보기 버전이므로 다음 방법 중 하나를 사용하여 옵트인해야 합니다.

  1. SAM_CLI_BETA_PYTHON_UV=1 환경 변수를 사용합니다.

  2. 다음을 samconfig.toml 파일에 추가합니다.

    [default.build.parameters] beta_features = true [default.sync.parameters] beta_features = true
  3. 지원되는 AWS SAMCLI 명령을 사용할 때는 이 --beta-features 옵션을 사용합니다. 예제:

    $ sam build --beta-features
  4. AWS SAMCLI에 옵트인하라는 메시지가 표시되면 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

1단계: AWS SAM 템플릿 구성

다음을 사용하여 AWS SAM 템플릿을 구성합니다.

  • BuildMethodpython-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 ...

예제

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 SAMCLI는 .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가 기본적으로 사용됩니다.