

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

# 使用自定义构建 AWS SAM
<a name="building-lambda-functions"></a>

您可以自定义构建以包含特定的 Lambda 函数或 Lambda 层。函数是一种资源，您可以对其调用以在 Lambda 中运行您的代码。Lambda 层允许您从 Lambda 函数中提取代码，然后可以跨多个 Lambda 函数中重复使用这些代码。当您想专注于开发和部署单个无服务器函数，且免于管理共享依赖项或资源的复杂性时，则可以选择使用特定的 Lambda 函数自定义构建。此外，您可以选择构建 Lambda 层来帮助您缩小部署包的大小，将核心函数逻辑与依赖项分开，并允许您在多个函数之间共享依赖项。

本节中的主题探讨了您可以用来构建 Lambda 函数的一些不同方法。 AWS SAM其中包括使用客户运行时构建 Lambda 函数和构建 Lambda 层。自定义运行时允许您安装和使用开发人员指南中 Lambda 运行时中 AWS Lambda 未列出的语言。这样，您可以创建用于运行无服务器函数和应用程序的专业执行环境。仅构建 Lambda 层（而不是构建整个应用程序）可以在几个方面使您受益。它可以帮助您缩小部署包的大小，将核心函数逻辑与依赖项分开，并允许您在多个函数之间共享依赖项。

有关函数的更多信息，请参阅《AWS Lambda 开发人员指南》**中的 [Lambda 概念](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-concepts.html)。

**Topics**
+ [在 esbuild 中构建 Node.js Lambda 函数 AWS SAM](serverless-sam-cli-using-build-typescript.md)
+ [使用原生 AOT 编译功能构建.NET Lambda 函数 AWS SAM](build-dotnet7.md)
+ [使用 in 构建 Rust Lambda 函数 Cargo Lambda AWS SAM](building-rust.md)
+ [使用输入构建 Python Lambda 函数 uv AWS SAM](building-python-uv.md)
+ [在中使用自定义运行时构建 Lambda 函数 AWS SAM](building-custom-runtimes.md)
+ [在中构建 Lambda 图层 AWS SAM](building-layers.md)

# 在 esbuild 中构建 Node.js Lambda 函数 AWS SAM
<a name="serverless-sam-cli-using-build-typescript"></a>

要构建和打包 Node.js AWS Lambda 函数，可以将与 esbuild JavaScript 捆绑器 AWS SAMCLI一起使用。esbuild 捆绑器支持你写入的 Lambda 函数。 TypeScript

要使用 esbuild 构建 Node.js Lambda 函数，请向您的 `AWS:Serverless::Function` 资源添加一个 `Metadata` 对象并为 `BuildMethod` 指定 `esbuild`。当你运行**sam build**命令时， AWS SAM 使用 esbuild 来捆绑你的 Lambda 函数代码。

## 元数据属性
<a name="serverless-sam-cli-using-build-typescript-metadata"></a>

`Metadata` 对象针对 esbuild 支持以下属性：

### BuildMethod
<a name="serverless-sam-cli-using-build-typescript-metadata-buildmethod"></a>

为应用程序指定捆绑程序。`esbuild` 是唯一受支持的值。

### BuildProperties
<a name="serverless-sam-cli-using-build-typescript-metadata-buildproperties"></a>

为 Lambda 函数代码指定构建属性。

`BuildProperties` 对象针对 esbuild 支持以下属性：所有属性均为可选属性。默认情况下， AWS SAM 使用您的 Lambda 函数处理程序作为入口点。

**EntryPoints**  
为应用程序指定入口点。

**外部**  
指定要从构建中省略的程序包列表。有关更多信息，请参阅 *esbuild 网站*上的[外部](https://esbuild.github.io/api/#external)。

**Format**  
指定应用程序中生成 JavaScript 文件的输出格式。有关更多信息，请参阅 *esbuild 网站*中的[格式](https://esbuild.github.io/api/#format)。

**加载程序**  
指定用于加载给定文件类型的数据的配置列表。

**MainFields**  
指定解析程序包时要尝试导入哪些 `package.json` 字段。默认值为 `main,module`。

**缩小**  
指定是否缩小捆绑的输出代码。默认值为 `true`。

**OutExtension**  
自定义 esbuild 生成的文件的扩展名。有关更多信息，请参阅 *esbuild 网站*上的[向外扩展](https://esbuild.github.io/api/#out-extension)。

**源映射**  
指定捆绑程序是否生成源映射文件。默认值为 `false`。  
如果设置为 `true`，则 `NODE_OPTIONS: --enable-source-maps` 会附加到 Lambda 函数的环境变量中，生成源映射并将其包含在函数中。  
或者，如果 `NODE_OPTIONS: --enable-source-maps` 包含在函数的环境变量中，则 `Sourcemap` 会自动设置为 `true`。  
发生冲突时，`Sourcemap: false` 优先于 `NODE_OPTIONS: --enable-source-maps`。  
默认情况下，Lambda 使用 AWS Key Management Service (AWS KMS) 加密所有静态环境变量。使用源映射时，要成功部署，您的函数的执行角色必须具有执行 `kms:Encrypt` 操作的权限。

**SourcesContent**  
指定是否在源映射文件中包含源代码。当 `Sourcemap` 设置为 `'true'` 时，可配置此属性。  
+ 指定 `SourcesContent: 'true'` 包含所有源代码。
+ 指定 `SourcesContent: 'false'` 排除所有源代码。这样可以缩小源映射文件的大小，缩短启动时间，从而在生产中非常有用。但是，调试器中将无法使用源代码。
默认值为 `SourcesContent: true`。  
有关更多信息，请参阅 *esbuild 网站*中的[源内容](https://esbuild.github.io/api/#sources-content)。

**Target**  
指定目标 ECMAScript 版本。默认值为 `es2020`。

## TypeScript Lambda 函数示例
<a name="serverless-sam-cli-using-build-typescript-example"></a>

以下示例 AWS SAM 模板片段使用 esbuild 根据中的代码创建 Node.js Lambda 函数。 TypeScript `hello-world/app.ts`

```
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello-world/
      Handler: app.handler
      Runtime: nodejs20.x
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api 
          Properties:
            Path: /hello
            Method: get
      Environment:
        Variables:
          NODE_OPTIONS: --enable-source-maps
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Format: esm
        Minify: false
        OutExtension:
          - .js=.mjs
        Target: "es2020"
        Sourcemap: true
        EntryPoints: 
          - app.ts
        External:
          - "<package-to-exclude>"
```

# 使用原生 AOT 编译功能构建.NET Lambda 函数 AWS SAM
<a name="build-dotnet7"></a>

使用 AWS Serverless Application Model (AWS SAM) 构建和打包您的.NET 8 AWS Lambda 函数，利用原生 Ahead-of-Time (AOT) 编译来缩短 AWS Lambda 冷启动时间。

**Topics**
+ [.NET 8 本机 AOT 概览](#build-dotnet7-overview)
+ [AWS SAM 与您的.NET 8 Lambda 函数一起使用](#build-dotnet7-sam)
+ [安装必备组件](#build-dotnet7-prerequisites)
+ [在模板中定义.NET 8 Lambda 函数 AWS SAM](#build-dotnet7-sam-define)
+ [使用 AWS SAM CLI 构建应用程序](#build-dotnet7-sam-build)
+ [了解详情](#build-dotnet7-learn-more)

## .NET 8 本机 AOT 概览
<a name="build-dotnet7-overview"></a>

过去，.NET Lambda 函数的冷启动时间会影响无服务器应用程序的用户体验、系统延迟和使用成本。凭借 .NET 本机 AOT 编译，您可以缩短 Lambda 函数的冷启动时间。要了解有关.NET 8 原生 AOT 的更多信息，请参阅在 D [otnet 存储库中使用原生 A](https://github.com/dotnet/runtime/tree/main/src/coreclr/nativeaot#readme) *O GitHub * T。

## AWS SAM 与您的.NET 8 Lambda 函数一起使用
<a name="build-dotnet7-sam"></a>

执行以下操作，使用 AWS Serverless Application Model （AWS SAM）配置 .NET 8 Lambda 函数：
+ 在开发计算机上安装必备组件。
+ 在模板中定义.NET 8 Lambda 函数。 AWS SAM 
+ 使用构建您的应用程序 AWS SAMCLI。

## 安装必备组件
<a name="build-dotnet7-prerequisites"></a>

以下是所需的先决条件：
+ 的 AWS SAMCLI
+ .NET Core CLI
+ Amazon.Lambda.Tools .NET Core Global Tool
+ Docker

**安装 AWS SAM CLI**

1. 要检查是否已安装 AWS SAM CLI，请运行以下命令：

   ```
   sam --version
   ```

1. 要安装 AWS SAMCLI，请参阅[安装 AWS SAM CLI](install-sam-cli.md)。

1. 要升级的已安装版本 AWS SAMCLI，请参阅[升级 AWS SAM CLI](manage-sam-cli-versions.md#manage-sam-cli-versions-upgrade)。

**安装 .NET Core CLI**

1. 要下载和安装 .NET Core CLI，请参阅从 Microsoft 网站[下载 .NET](https://dotnet.microsoft.com/download)。

1. 有关 .NET Core CLI 的更多信息，请参阅*《AWS Lambda 开发人员指南》*中的 [.NET Core CLI](https://docs.aws.amazon.com/lambda/latest/dg/csharp-package-cli.html)。

**安装 Amazon.Lambda.Tools .NET Core Global Tool**

1. 运行如下命令：

   ```
   dotnet tool install -g Amazon.Lambda.Tools
   ```

1. 如果您已安装该工具，请确保该工具是使用以下命令的最新版本：

   ```
   dotnet tool update -g Amazon.Lambda.Tools
   ```

1. 有关 Amazon.Lambda.Tools .NET 核心全球工具的更多信息，请参阅上的.NE [T CLI 存储AWS 库](https://github.com/aws/aws-extensions-for-dotnet-cli)扩展。 GitHub

**安装 Docker**
+ 使用本机 AOT 进行构建需要安装 Docker。有关安装说明，请参阅[安装 Docker 以与 AWS SAM CLI 一起使用](install-docker.md)。

## 在模板中定义.NET 8 Lambda 函数 AWS SAM
<a name="build-dotnet7-sam-define"></a>

要定义. NET8 在您的 AWS SAM 模板中使用 Lambda 函数，请执行以下操作：

1. 从您选择的起始目录运行以下命令：

   ```
   sam init
   ```

1. 选择 `AWS Quick Start Templates` 以选择起始模板。

1. 选择 `Hello World Example`模板。

1. 通过输入 `n`，选择不使用最流行的运行时和包类型。

1. 对于运行时，选择 `dotnet8`。

1. 对于包类型，选择 `Zip`。

1. 对于您的入门模板，选择 `Hello World Example using native AOT`。

**安装 Docker**
+ 使用本机 AOT 进行构建需要安装 Docker。有关安装说明，请参阅[安装 Docker 以与 AWS SAM CLI 一起使用](install-docker.md)。

```
Resources:
HelloWorldFunction:
  Type: AWS::Serverless::Function
  Properties:
    CodeUri: ./src/HelloWorldAot/
    Handler: bootstrap
    Runtime: dotnet8
    Architectures:
      - x86_64
    Events:
      HelloWorldAot:
        Type: Api 
        Properties:
          Path: /hello
          Method: get
```

**注意**  
如果的`Event`属性设置`AWS::Serverless::Function`为`Api`，但未指定该`RestApiId`属性，则 AWS SAM 生成`AWS::ApiGateway::RestApi` CloudFormation 资源。

## 使用 AWS SAM CLI 构建应用程序
<a name="build-dotnet7-sam-build"></a>

 在您的项目根目录中，运行 `sam build` 命令以开始构建应用程序。如果 `PublishAot` 属性已在 .NET 8 项目文件中定义，则 AWS SAM CLI 将使用本机 AOT 编译进行构建。要了解有关 `PublishAot` 属性的更多信息，请参阅 Microsoft *.NET 文档*中的[本机 AOT 部署](https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/)。

要构建您的函数， AWS SAM CLI 会调用使用 Amazon.Lambda.Tools .NET Core Global Tool 的 .NET Core CLI。

**注意**  
构建时，如果项目所在目录或父目录中存在 `.sln` 文件，则包含该 `.sln` 文件的目录将挂载到容器中。如果找不到 `.sln` 文件，则只会挂载项目文件夹。因此，如果您要构建多项目应用程序，请确保 `.sln` 文件位于属性中。

## 了解详情
<a name="build-dotnet7-learn-more"></a>

有关构建 .NET 8 Lambda 函数的更多信息，请参阅 [Introducing the .NET 8 runtime for AWS Lambda](https://aws.amazon.com/blogs/compute/introducing-the-net-8-runtime-for-aws-lambda/)。

有关 **sam build** 命令的参考，请参阅 [sam build](sam-cli-command-reference-sam-build.md)。

# 使用 in 构建 Rust Lambda 函数 Cargo Lambda AWS SAM
<a name="building-rust"></a>


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

使用 AWS Serverless Application Model 命令行界面 (AWS SAMCLI) 和 Rust AWS Lambda 函数。

**Topics**
+ [先决条件](#building-rust-prerequisites)
+ [配置 AWS SAM 为与 Rust Lambda 函数一起使用](#building-rust-configure)
+ [示例](#building-rust-examples)

## 先决条件
<a name="building-rust-prerequisites"></a>

**Rust 语言**  
要安装 Rust，请参阅 *Rust 语言网站*上的[安装 Rust](https://www.rust-lang.org/tools/install)。

**Cargo Lambda**  
 AWS SAM CLI 要求安装 [https://www.cargo-lambda.info/guide/what-is-cargo-lambda.html](https://www.cargo-lambda.info/guide/what-is-cargo-lambda.html)，是 Cargo 的子命令。有关安装说明，请参阅 *Cargo Lambda 文档*中的[安装](https://www.cargo-lambda.info/guide/installation.html)。

**Docker**  
构建和测试 Rust Lambda 函数需要 Docker。有关安装说明，请参阅[安装 Docker](install-docker.md)。

**选择加入 AWS SAM CLI 测试版功能**  
由于此功能为预览版，因此您必须使用以下方法之一选择加入：  

1. 使用环境变量：`SAM_CLI_BETA_RUST_CARGO_LAMBDA=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 "rust-cargolambda" 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 为与 Rust Lambda 函数一起使用
<a name="building-rust-configure"></a>

### 第 1 步：配置您的 AWS SAM 模板
<a name="building-rust-configure-template"></a>

使用以下内容配置您的 AWS SAM 模板：
+ **二进制** – 可选。指定模板何时包含多个 Rust Lambda 函数。
+ **BuildMethod** – `rust-cargolambda`.
+ **CodeUri**— `Cargo.toml` 文件路径。
+ **处理程序** – `bootstrap`.
+ **运行时系统** – `provided.al2`.

要了解有关自定义运行时的更多信息，请参阅《*AWS Lambda 开发者*指南[AWS Lambda 》中的自定义运行时](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html)。

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

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Metadata:
      BuildMethod: rust-cargolambda
      BuildProperties: function_a
    Properties:
      CodeUri: ./rust_app
      Handler: bootstrap
      Runtime: provided.al2
...
```

### 第 2 步：将 AWS SAM CLI 与 Rust Lambda 函数一起使用
<a name="building-rust-configure-cli"></a>

在 AWS SAM 模板中使用任意 AWS SAMCLI命令。有关更多信息，请参阅 [AWS SAM CLI](using-sam-cli.md)。

## 示例
<a name="building-rust-examples"></a>

### Hello World 示例
<a name="building-rust-examples-hello"></a>

**在此示例中，我们使用 Rust 作为运行时系统来构建示例 Hello World 应用程序。**

首先，我们使用 `sam init` 初始化新的无服务器应用程序。在交互式流程中，我们选择 **Hello World 应用程序**并选择 **Rust** 运行时系统。

```
$ sam init
...
Which template source would you like to use?
        1 - AWS Quick Start Templates
        2 - Custom Template Location
Choice: 1

Choose an AWS Quick Start application template
        1 - Hello World Example
        2 - Multi-step workflow
        3 - Serverless API
        ...
Template: 1

Use the most popular runtime and package type? (Python and zip) [y/N]: ENTER

Which runtime would you like to use?
        1 - dotnet8
        2 - dotnet6
        3 - go (provided.al2)
        ...
        18 - python3.11
        19 - python3.10
        20 - ruby3.3
        21 - ruby3.2
        22 - rust (provided.al2)
        23 - rust (provided.al2023)
Runtime: 22

Based on your selections, the only Package type available is Zip.
We will proceed to selecting the Package type as Zip.

Based on your selections, the only dependency manager available is cargo.
We will proceed copying the template using cargo.

Would you like to enable X-Ray tracing on the function(s) in your application?  [y/N]: ENTER

Would you like to enable monitoring using CloudWatch Application Insights?
For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: ENTER

Project name [sam-app]: hello-rust

    -----------------------
    Generating application:
    -----------------------
    Name: hello-rust
    Runtime: rust (provided.al2)
    Architectures: x86_64
    Dependency Manager: cargo
    Application Template: hello-world
    Output Directory: .
    Configuration file: hello-rust/samconfig.toml
    
    Next steps can be found in the README file at hello-rust/README.md
        

Commands you can use next
=========================
[*] Create pipeline: cd hello-rust && sam pipeline init --bootstrap
[*] Validate SAM template: cd hello-rust && sam validate
[*] Test Function in the Cloud: cd hello-rust && sam sync --stack-name {stack-name} --watch
```

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

```
hello-rust
├── README.md
├── events
│   └── event.json
├── rust_app
│   ├── Cargo.toml
│   └── src
│       └── main.rs
├── samconfig.toml
└── template.yaml
```

在我们的 AWS SAM 模板中，我们的Rust函数定义如下：

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function 
    Metadata:
      BuildMethod: rust-cargolambda 
    Properties:
      CodeUri: ./rust_app 
      Handler: bootstrap   
      Runtime: provided.al2
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api
            Path: /hello
            Method: get
```

接下来，运行 `sam build` 以构建应用程序并准备部署。 AWS SAM CLI 创建一个 `.aws-sam` 目录并在其中整理构建构件。函数是使用 Cargo Lambda 构建，并以可执行二进制文件的形式存储于 `.aws-sam/build/HelloWorldFunction/bootstrap`。

**注意**  
如果您计划在 MacOS 中运行 **sam local invoke** 命令，则需要在调用之前构建不同的函数。要执行此操作，请使用以下命令：  
**SAM\$1BUILD\$1MODE=debug sam build**
仅在完成本地测试时才需要此命令。在为部署构建时，不建议这样做。

```
hello-rust$ sam build
Starting Build use cache
Build method "rust-cargolambda" 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-rust/rust_app runtime: provided.al2 metadata: {'BuildMethod': 'rust-cargolambda'} architecture: x86_64 functions: HelloWorldFunction
Running RustCargoLambdaBuilder:CargoLambdaBuild
Running RustCargoLambdaBuilder:RustCopyAndRename

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

接下来，使用 `sam deploy --guided` 部署应用程序。

```
hello-rust$ sam deploy --guided

Configuring SAM deploy
======================

        Looking for config file [samconfig.toml] :  Found
        Reading default arguments  :  Success

        Setting default arguments for 'sam deploy'
        =========================================
        Stack Name [hello-rust]: ENTER
        AWS Region [us-west-2]: ENTER
        #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
        Confirm changes before deploy [Y/n]: ENTER
        #SAM needs permission to be able to create roles to connect to the resources in your template
        Allow SAM CLI IAM role creation [Y/n]: ENTER
        #Preserves the state of previously provisioned resources when an operation fails
        Disable rollback [y/N]: ENTER
        HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
        Save arguments to configuration file [Y/n]: ENTER
        SAM configuration file [samconfig.toml]: ENTER
        SAM configuration environment [default]: ENTER

        Looking for resources needed for deployment:

        ...

        Uploading to hello-rust/56ba6585d80577dd82a7eaaee5945c0b  817973 / 817973  (100.00%)

        Deploying with following values
        ===============================
        Stack name                   : hello-rust
        Region                       : us-west-2
        Confirm changeset            : True
        Disable rollback             : False
        Deployment s3 bucket         : aws-sam-cli-managed-default-samclisam-s3-demo-bucket-1a4x26zbcdkqr
        Capabilities                 : ["CAPABILITY_IAM"]
        Parameter overrides          : {}
        Signing Profiles             : {}

Initiating deployment
=====================

        Uploading to hello-rust/a4fc54cb6ab75dd0129e4cdb564b5e89.template  1239 / 1239  (100.00%)


Waiting for changeset to be created..

CloudFormation stack changeset
---------------------------------------------------------------------------------------------------------
Operation                  LogicalResourceId          ResourceType               Replacement              
---------------------------------------------------------------------------------------------------------
+ Add                      HelloWorldFunctionHelloW   AWS::Lambda::Permission    N/A                      
                           orldPermissionProd                                                             
...                    
---------------------------------------------------------------------------------------------------------

Changeset created successfully. arn:aws:cloudformation:us-west-2:012345678910:changeSet/samcli-deploy1681427201/f0ef1563-5ab6-4b07-9361-864ca3de6ad6


Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y

2023-04-13 13:07:17 - Waiting for stack create/update to complete

CloudFormation events from stack operations (refresh every 5.0 seconds)
---------------------------------------------------------------------------------------------------------
ResourceStatus             ResourceType               LogicalResourceId          ResourceStatusReason     
---------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS         AWS::IAM::Role             HelloWorldFunctionRole     -                        
CREATE_IN_PROGRESS         AWS::IAM::Role             HelloWorldFunctionRole     Resource creation        
...
---------------------------------------------------------------------------------------------------------

CloudFormation outputs from deployed stack
---------------------------------------------------------------------------------------------------------
Outputs                                                                                                 
---------------------------------------------------------------------------------------------------------
Key                 HelloWorldFunctionIamRole                                                           
Description         Implicit IAM Role created for Hello World function                                  
Value               arn:aws:iam::012345678910:role/hello-rust-HelloWorldFunctionRole-10II2P13AUDUY      

Key                 HelloWorldApi                                                                       
Description         API Gateway endpoint URL for Prod stage for Hello World function                    
Value               https://ggdxec9le9.execute-api.us-west-2.amazonaws.com/Prod/hello/                  

Key                 HelloWorldFunction                                                                  
Description         Hello World Lambda Function ARN                                                     
Value               arn:aws:lambda:us-west-2:012345678910:function:hello-rust-HelloWorldFunction-       
yk4HzGzYeZBj                                                                                            
---------------------------------------------------------------------------------------------------------


Successfully created/updated stack - hello-rust in us-west-2
```

为了进行测试，我们可以使用 API 端点调用 Lambda 函数。

```
$ curl https://ggdxec9le9.execute-api.us-west-2.amazonaws.com/Prod/hello/
Hello World!%
```

要在本地测试函数，首先我们要确保函数的 `Architectures` 属性与本地计算机相匹配。

```
...
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Metadata:
      BuildMethod: rust-cargolambda # More info about Cargo Lambda: https://github.com/cargo-lambda/cargo-lambda
    Properties:
      CodeUri: ./rust_app   # Points to dir of Cargo.toml
      Handler: bootstrap    # Do not change, as this is the default executable name produced by Cargo Lambda
      Runtime: provided.al2
      Architectures:
        - arm64
...
```

由于我们在此示例中将基础设施从 `x86_64` 修改为 `arm64`，因此我们运行 `sam build` 以更新构建构件。然后运行 `sam local invoke` 在本地调用函数。

```
hello-rust$ sam local invoke
Invoking bootstrap (provided.al2)
Local image was not found.
Removing rapid images for repo public.ecr.aws/sam/emulation-provided.al2
Building image.....................................................................................................................................
Using local image: public.ecr.aws/lambda/provided:al2-rapid-arm64.

Mounting /Users/.../hello-rust/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container
START RequestId: fbc55e6e-0068-45f9-9f01-8e2276597fc6 Version: $LATEST
{"statusCode":200,"body":"Hello World!"}END RequestId: fbc55e6e-0068-45f9-9f01-8e2276597fc6
REPORT RequestId: fbc55e6e-0068-45f9-9f01-8e2276597fc6  Init Duration: 0.68 ms  Duration: 130.63 ms     Billed Duration: 131 ms     Memory Size: 128 MB     Max Memory Used: 128 MB
```

### 单个 Lambda 函数项目
<a name="building-rust-examples-single"></a>

**以下是一个包含单个 Rust Lambda 函数的无服务器应用程序的示例。**

项目目录结构：

```
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── template.yaml
```

AWS SAM 模板：

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Metadata:
      BuildMethod: rust-cargolambda
    Properties:
      CodeUri: ./             
      Handler: bootstrap
      Runtime: provided.al2
...
```

### 多个 Lambda 函数项目
<a name="building-rust-examples-multiple"></a>

**以下是一个包含多个 Rust Lambda 函数的无服务器应用程序的示例。**

项目目录结构：

```
.
├── Cargo.lock
├── Cargo.toml
├── src
│   ├── function_a.rs
│   └── function_b.rs
└── template.yaml
```

AWS SAM 模板：

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
Resources:
  FunctionA:
    Type: AWS::Serverless::Function
    Metadata:
      BuildMethod: rust-cargolambda
      BuildProperties:
        Binary: function_a 
    Properties:
      CodeUri: ./           
      Handler: bootstrap     
      Runtime: provided.al2
  FunctionB:
    Type: AWS::Serverless::Function
    Metadata:
      BuildMethod: rust-cargolambda
      BuildProperties:
        Binary: function_b
    Properties:
      CodeUri: ./
      Handler: bootstrap
      Runtime: provided.al2
```

`Cargo.toml` 文件：

```
[package]
name = "test-handler"
version = "0.1.0"
edition = "2021"

[dependencies]
lambda_runtime = "0.6.0"
serde = "1.0.136"
tokio = { version = "1", features = ["macros"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }

[[bin]]
name = "function_a"
path = "src/function_a.rs"

[[bin]]
name = "function_b"
path = "src/function_b.rs"
```

# 使用输入构建 Python Lambda 函数 uv AWS SAM
<a name="building-python-uv"></a>


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

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

**Topics**
+ [先决条件](#building-python-uv-prerequisites)
+ [配置 AWS SAM 为与 Python Lambda 函数一起使用以及 uv](#building-python-uv-configure)
+ [示例](#building-python-uv-examples)

## 先决条件
<a name="building-python-uv-prerequisites"></a>

**Python**  
要安装 Python，请参阅在 [Python 网站上下载](https://www.python.org/downloads/) *Python*。

**uv**  
 AWS SAMCLI需要安装速度极快的 [https://docs.astral.sh/uv/](https://docs.astral.sh/uv/)Python 包安装程序和解析器。有关安装说明，请参阅 *uv 文档*中的[安装](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
   ```

## 配置 AWS SAM 为与 Python Lambda 函数一起使用以及 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`。
+ **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
...
```

## 示例
<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`部分中的每个函数配置的。模板中的每个函数都可以使用不同的构建方法，从而允许您在同一个 AWS SAM 模板中将uv基于的函数与`pip`基于的函数混合使用。如果未指定构建方法，`pip`则默认使用。

# 在中使用自定义运行时构建 Lambda 函数 AWS SAM
<a name="building-custom-runtimes"></a>

您可以使用 `sam build` 命令构建 Lambda 函数所需的自定义运行时系统。您可以通过为函数指定 `Runtime: provided` 来声明 Lambda 函数以使用自定义运行时系统。

要构建自定义运行时系统，请使用 `BuildMethod: makefile` 条目声明 `Metadata` 资源属性。您提供自定义 makefile，在其中声明包含运行时构建命令的表单 `build-function-logical-id` 的构建目标。如有必要，您的 makefile 负责编译自定义运行时系统，并将构建构件复制到工作流程中后续步骤所需的适当位置。Makefile 的位置由函数资源的 `CodeUri` 属性指定，并且必须命名为 `Makefile`。

## 示例
<a name="building-custom-runtimes-examples"></a>

### 示例 1：用 Rust 编写的函数的自定义运行时系统
<a name="building-custom-runtimes-examples-rust"></a>

**注意**  
我们建议使用 Cargo Lambda 构建 Lambda 函数。要了解更多信息，请参阅[使用 in 构建 Rust Lambda 函数 Cargo Lambda AWS SAM](building-rust.md)。

以下 AWS SAM 模板声明了一个函数，该函数为用 Rust 编写的 Lambda 函数使用自定义运行时，并指示`sam build`为构建目标执行命令。`build-HelloRustFunction`

```
Resources:
  HelloRustFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: HelloRust
      Handler: bootstrap.is.real.handler
      Runtime: provided
      MemorySize: 512
      CodeUri: .
    Metadata:
      BuildMethod: makefile
```

以下 makefile 包含构建目标和将要执行的命令。请注意，`CodeUri` 属性设置为 `.`，因此 makefile 必须位于项目根目录（即与应用程序 AWS SAM 模板文件相同的目录）。文件名必须是 `Makefile`。

```
build-HelloRustFunction:
	cargo build --release --target x86_64-unknown-linux-musl
	cp ./target/x86_64-unknown-linux-musl/release/bootstrap $(ARTIFACTS_DIR)
```

有关设置开发环境以执行前面 `makefile` 中的 `cargo build` 命令的更多信息，请参阅博客文章 [AWS Lambda的 Rust 运行时系统](https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-lambda/)。

### 示例 2：适用于 Python3.12 的 makefile 生成器（替代使用捆绑生成器）
<a name="building-custom-runtimes-examples-python"></a>

您可能需要使用未包含在捆绑生成器中的库或模块。此示例显示了带有 makefile 生成器的 Python3.12 运行时的 AWS SAM 模板。

```
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.12
    Metadata:
      BuildMethod: makefile
```

以下 makefile 包含构建目标和将要执行的命令。请注意，`CodeUri` 属性设置为 `hello_world`，因此 makefile 必须位于 `hello_world` 子目录的根目录中，并且文件名必须为 `Makefile`。

```
build-HelloWorldFunction:
	cp *.py $(ARTIFACTS_DIR)
	cp requirements.txt $(ARTIFACTS_DIR)
	python -m pip install -r requirements.txt -t $(ARTIFACTS_DIR)
	rm -rf $(ARTIFACTS_DIR)/bin
```

# 在中构建 Lambda 图层 AWS SAM
<a name="building-layers"></a>



您可以使用 AWS SAM 来构建自定义 Lambda 层。Lambda 层允许您从 Lambda 函数中提取代码，然后可以在多个 Lambda 函数中重复使用这些代码。仅构建 Lambda 层（而不是构建整个应用程序）可以在几个方面使您受益。它可以帮助您缩小部署包的大小，将核心函数逻辑与依赖项分开，并允许您在多个函数之间共享依赖项。有关层的信息，请参阅*《AWS Lambda 开发人员指南》*中的 [AWS Lambda 层](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html)。

## 如何在中构建 Lambda 层 AWS SAM
<a name="w2aac18c23c19c34b7"></a>

**注意**  
在构建 Lambda 层之前，必须先在模板中写一个 Lambda 层。 AWS SAM 有关执行此操作的信息和示例，请参阅[使用带有 Lambda 层的 Lambda 层来提高效率 AWS SAM](serverless-sam-cli-layers.md)。

要构建自定义层，请在您的 AWS Serverless Application Model (AWS SAM) 模板文件中对其进行声明，并在`Metadata`资源属性部分中`BuildMethod`加入一个条目。`BuildMethod` 的有效值是 [AWS Lambda 运行时系统](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html)或 `makefile` 的标识符。纳入一个 `BuildArchitecture` 条目以指定您的层支持的指令集架构。`BuildArchitecture` 的有效值是 [Lambda 指令集架构](https://docs.aws.amazon.com/lambda/latest/dg/foundation-arch.html)。

如果指定 `makefile`，请提供自定义 makefile，在其中声明包含层构建命令的表单 `build-layer-logical-id` 的构建目标。如有必要，您的 makefile 负责编译层，并将构建构件复制到工作流程中后续步骤所需的适当位置。Makefile 的位置由层资源的 `ContentUri` 属性指定，并且必须命名为 `Makefile`。

**注意**  
创建自定义图层时，需要 AWS Lambda 依靠环境变量来查找图层代码。Lambda 运行时系统包含将您的层代码复制到的 `/opt` 目录中的路径。项目的构建构件文件夹结构必须与运行时系统的预期文件夹结构相匹配，这样才能找到自定义层代码。  
例如，对于 Python，您可以将代码放在 `python/` 子目录中。对于 NodeJS，您可以将代码放在 `nodejs/node_modules/` 子目录中。  
有关更多信息，请参阅*《AWS Lambda 开发人员指南》*中的[在层中包括库依赖项](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path)。

以下是 `Metadata` 资源属性部分的示例。

```
    Metadata:
      BuildMethod: python3.12
      BuildArchitecture: arm64
```

**注意**  
如果您不包括`Metadata`资源属性部分，则 AWS SAM 不构建图层。相反，它会从层资源 `CodeUri` 属性中指定的位置复制构建构件。有关更多信息，请参阅 `AWS::Serverless::LayerVersion` 资源类型中 [ContentUri](sam-resource-layerversion.md#sam-layerversion-contenturi) 属性。

当包含`Metadata`资源属性部分时，您可以使用`sam build`命令来构建层，既可以作为独立对象，也可以作为 AWS Lambda 函数的依赖项。
+ ****作为独立对象。****您可能只想构建层对象，例如，当您在本地测试层的代码更改并且不需要构建整个应用程序时。要独立构建层，请使用 `sam build layer-logical-id` 命令指定层资源。
+ **作为 Lambda 函数的依赖项。**当您在同一 AWS SAM 模板文件的 Lambda 函数的 `Layers` 属性中包含层的逻辑 ID 时，该层就是该 Lambda 函数的依赖项。当该层还包括带有 `BuildMethod` 条目的 `Metadata` 资源属性部分时，您可以通过使用 `sam build` 命令构建整个应用程序或使用 `sam build function-logical-id` 命令指定函数资源来构建该层。

## 示例
<a name="building-applications-examples"></a>

### 模板示例 1：针对 Python 3.12 运行时环境构建一个层
<a name="building-applications-examples-python"></a>

以下示例 AWS SAM 模板基于 Python 3.12 运行时环境构建了一个层。

```
Resources:
  MyLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      ContentUri: my_layer
      CompatibleRuntimes:
        - python3.12
    Metadata:
      BuildMethod: python3.12   # Required to have AWS SAM build this layer
```

### 模板示例 2：使用自定义 makefile 构建层
<a name="building-applications-examples-makefile"></a>

以下示例 AWS SAM 模板使用自定义模板`makefile`来构建图层。

```
Resources:
  MyLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      ContentUri: my_layer
      CompatibleRuntimes:
        - python3.12
    Metadata:
      BuildMethod: makefile
```

以下 `makefile` 包含构建目标和将要执行的命令。请注意，`ContentUri` 属性设置为 `my_layer`，因此 makefile 必须位于 `my_layer` 子目录的根目录中，并且文件名必须为 `Makefile`。另请注意，构建工件被复制到`python/`子目录中，以便 AWS Lambda 能够找到层代码。

```
build-MyLayer:
  mkdir -p "$(ARTIFACTS_DIR)/python"
  cp *.py "$(ARTIFACTS_DIR)/python"
  python -m pip install -r requirements.txt -t "$(ARTIFACTS_DIR)/python"
```

**注意**  
当调用 `makefile` 时，将触发相应目标，并应将构件复制到暴露的环境变量 `$ARTIFACTS_DIR` 中。有关更多信息，请参阅[aws-lambda-builders 中的 GitHub](https://github.com/aws/aws-lambda-builders/blob/develop/aws_lambda_builders/workflows/custom_make/DESIGN.md)。

### sam 构建命令示例
<a name="building-applications-examples-commands"></a>

以下 `sam build` 命令构建包含 `Metadata` 资源属性部分的层。

```
# Build the 'layer-logical-id' resource independently
$ sam build layer-logical-id
            
# Build the 'function-logical-id' resource and layers that this function depends on
$ sam build function-logical-id

# Build the entire application, including the layers that any function depends on
$ sam build
```