

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

# 将 Guard 作为 AWS Lambda 功能安装
<a name="setting-up-lambda"></a>

你可以 AWS CloudFormation Guard 通过 Rust 软件包管理器 Cargo 进行安装。*Guard as* a function (`cfn-guard-lambda`) 是围绕 Guard (`cfn-guard`) 的轻量级封装器，可用作 Lambda AWS Lambda函数。

## 先决条件
<a name="guard-as-lambda-prerequisites"></a>

在将 Guard 作为 Lambda 函数安装之前，必须满足以下先决条件：
+ AWS Command Line Interface (AWS CLI) 配置了部署和调用 Lambda 函数的权限。有关更多信息，请参阅[配置 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)。
+  AWS Identity and Access Management (IAM) 中的 AWS Lambda 执行角色。有关更多信息，请参阅[AWS Lambda 执行角色](https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html)。
+ 在 CentOS/RHEL 环境中，将`musl-libc`软件包存储库添加到你的 yum 配置中。欲了解更多信息，请参阅 [ngompa](https://copr.fedorainfracloud.org/coprs/ngompa/musl-libc/) /musl-libc。

## 安装 Rust 软件包管理器
<a name="install-rust-and-cargo"></a>

Cargo 是 Rust 包管理器。完成以下步骤来安装 Rust，其中包括 Cargo。

1. 从终端运行以下命令，然后按照屏幕上的说明安装 Rust。

   ```
   curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
   ```

   1. （可选）对于 Ubuntu 环境，请运行以下命令。

     ```
     sudo apt-get update; sudo apt install build-essential
     ```

1. 配置您的`PATH`环境变量，然后运行以下命令。

   ```
   source $HOME/.cargo/env
   ```

## 将 Guard 安装为 Lambda 函数（Linux、macOS 或 Unix）
<a name="to-isntall-guard-as-a-lambda"></a>

要将 Guard 安装为 Lambda 函数，请完成以下步骤。

1. 在您的命令终端上运行以下命令。

   ```
   cargo install cfn-guard-lambda
   ```

   1. （可选）要确认将 Guard 安装为 Lambda 函数，请运行以下命令。

     ```
     cfn-guard-lambda --version
     ```

     该命令将返回以下输出。

     ```
     cfn-guard-lambda 3.1.2
     ```

1. 要安装`musl`支持，请运行以下命令。

   ```
   rustup target add x86_64-unknown-linux-musl
   ```

1. 使用构建`musl`，然后在终端中运行以下命令。

   ```
   cargo build --release --target x86_64-unknown-linux-musl
   ```

   对于[自定义运行时](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html)， AWS Lambda 需要一个名称为部署包.zip 文件`bootstrap`中的可执行文件。将生成的`cfn-lambda`可执行文件重命名为，`bootstrap`然后将其添加到.zip 存档中。

   1. 对于 macOS 环境，请在 Rust 项目的根目录或中创建您的货物配置文件。`~/.cargo/config`

     ```
     [target.x86_64-unknown-linux-musl]
     linker = "x86_64-linux-musl-gcc"
     ```

1. 切换到`cfn-guard-lambda`根目录。

   ```
   cd ~/.cargo/bin/cfn-guard-lambda
   ```

1. 在终端中运行以下命令。

   ```
   cp ./../target/x86_64-unknown-linux-musl/release/cfn-guard-lambda ./bootstrap && zip lambda.zip bootstrap && rm bootstrap
   ```

1. 运行以下命令以`cfn-guard`作为 Lambda 函数提交到您的账户。

   ```
   aws lambda create-function --function-name {{cfnGuard}} \
    --handler guard.handler \
    --zip-file fileb://./lambda.zip \
    --runtime provided \
    --role arn:aws:iam::{{444455556666}}:role/your_lambda_execution_role \
    --environment Variables={RUST_BACKTRACE=1} \
    --tracing-config Mode=Active
   ```

## 构建 Guard 并将其作为 Lambda 函数运行
<a name="build-and-run-lambda"></a>

要调用作`cfn-guard-lambda`为 Lambda 函数提交的，请运行以下命令。

```
aws lambda invoke --function-name {{cfnGuard}} \
  --payload '{"data":"{{input data}}","rules":["{{rule1}}","{{rule2}}"]}' \
  output.json
```

## 调用 Lambda 函数请求结构
<a name="calling-the-lambda-function"></a>

`cfn-guard-lambda`要求填写以下字段：
+ `data`— YAML 或 JSON 模板的字符串版本
+ `rules`— 规则集文件的字符串版本