

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 安裝 Guard 做為 AWS Lambda 函數
<a name="setting-up-lambda"></a>

您可以透過 Rust 套件管理員 AWS CloudFormation Guard Cargo 安裝 。*Guard 作為 AWS Lambda*函數 (`cfn-guard-lambda`) 是 Guard (`cfn-guard`) 周圍的輕量包裝函式，可用作 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/musl-libc](https://copr.fedorainfracloud.org/coprs/ngompa/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 需要部署套件 `bootstrap`.zip 檔案中名稱為 的可執行檔。將產生的可執行檔重新命名為 `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. 執行下列命令，以 Lambda 函數`cfn-guard`的形式提交至您的帳戶。

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

若要叫用以 Lambda 函數`cfn-guard-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` – 規則集檔案的字串版本