

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

# 設定 AWS CloudFormation Guard
<a name="setting-up"></a>

AWS CloudFormation Guard 是開放原始碼命令列界面 (CLI)。它為您提供簡單、特定網域的語言，以撰寫政策規則，並根據這些規則驗證其結構化階層式 JSON 和 YAML 資料。這些規則可以代表與安全性、合規性等相關的公司政策指導方針。結構化階層式資料可以代表描述為程式碼的雲端基礎設施。例如，您可以建立規則，以確保一律在其 CloudFormation 範本中建立加密的 Amazon Simple Storage Service (Amazon S3) 儲存貯體模型。

下列主題提供如何使用您選擇的作業系統或 AWS Lambda 函數安裝 Guard 的相關資訊。

**Topics**
+ [安裝 Guard for Linux 和 macOS](setting-up-linux.md)
+ [安裝 Guard for Windows](setting-up-windows.md)
+ [安裝 Guard 做為 AWS Lambda 函數](setting-up-lambda.md)

# 安裝 Guard for Linux 和 macOS
<a name="setting-up-linux"></a>

您可以使用預先建置的發行二進位檔、Cargo 或透過 Homebrew， AWS CloudFormation Guard 為 Linux 和 macOS 安裝 。

## 從預先建置的發行二進位檔安裝 Guard
<a name="install-pre-built-binaries"></a>

使用下列程序從預先建置的二進位檔安裝 Guard。

1. 開啟終端機，並執行下列命令。

   ```
   curl --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/aws-cloudformation/cloudformation-guard/main/install-guard.sh | sh
   ```

1. 執行下列命令來設定您的`PATH`變數。

   ```
   export PATH=~/.guard/bin:$PATH
   ```

   *結果：*您已成功安裝 Guard 並設定`PATH`變數。

   1. （選用） 若要確認 Guard 的安裝，請執行下列命令。

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

     命令會傳回下列輸出：

     ```
     cfn-guard 3.1.2
     ```

## 從 Cargo 安裝 Guard
<a name="install-guard-from-cargo"></a>

Cargo 是 Rust 套件管理員。完成下列步驟以安裝 Rust，其中包含 Cargo。然後，從 Cargo 安裝 Guard。

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

1. 安裝 Cargo 後，請執行下列命令來安裝 Guard。

   ```
   cargo install cfn-guard
   ```

   *結果*：您已成功安裝 Guard。

   1. （選用） 若要確認 Guard 的安裝，請執行下列命令。

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

     命令會傳回下列輸出：

     ```
     cfn-guard 3.1.2
     ```

## 從 Homebrew 安裝 Guard
<a name="install-guard-from-homebrew"></a>

Homebrew 是 macOS 和 Linux 的套件管理員。完成下列步驟以安裝 Homebrew。然後，從 Homebrew 安裝 Guard。

1. 從終端機執行下列命令，並依照畫面上的指示安裝 Homebrew。

   ```
   /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
   ```

1. 安裝 Homebrew 後，請執行下列命令來安裝 Guard。

   ```
   brew install cloudformation-guard
   ```

   *結果*：您已成功安裝 Guard。

   1. （選用） 若要確認 Guard 的安裝，請執行下列命令。

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

     命令會傳回下列輸出：

     ```
     cfn-guard 3.1.2
     ```

# 安裝 Guard for Windows
<a name="setting-up-windows"></a>

您可以透過 Cargo 或 Chocolatey AWS CloudFormation Guard 為 Windows 安裝 。

## 先決條件
<a name="w2aab8c16b5"></a>

若要從命令列界面建置 Guard，您必須安裝 Build Tools for Visual Studio 2019。

1. 從[適用於 Visual Studio 的建置工具 2019 網站下載 Microsoft Visual C\$1\$1 建置工具](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019)。

1. 執行安裝程式，然後選取預設值。

## 從 Cargo 安裝 Guard
<a name="install-guard-from-cargo"></a>

Cargo 是 Rust 套件管理員。完成下列步驟以安裝 Rust，其中包含 Cargo。然後，從 Cargo 安裝 Guard。

1. [下載 Rust](https://forge.rust-lang.org/infra/other-installation-methods.html#other-ways-to-install-rustup)，然後執行 **rustup-init.exe。**

1. 從命令提示字元中，選擇 **1**，這是預設選項。

   命令會傳回下列輸出：

   ```
   Rust is installed now. Great!
       
       To get started you may need to restart your current shell.
       This would reload its PATH environment variable to include
       Cargo's bin directory (%USERPROFILE%\.cargo\bin).
       
       Press the Enter key to continue.
   ```

1. 若要完成安裝，請按 **Enter** 鍵。

1. 安裝 Cargo 後，請執行下列命令來安裝 Guard。

   ```
   cargo install cfn-guard
   ```

   *結果*：您已成功安裝 Guard。

   1. （選用） 若要確認 Guard 的安裝，請執行下列命令。

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

     命令會傳回下列輸出：

     ```
     cfn-guard 3.1.2
     ```

## 從 Chocolatey 安裝 Guard
<a name="install-guard-from-chocolatey"></a>

Chocolatey 是 Windows 的套件管理員。完成下列步驟以安裝 Chocolatey。然後，從 Chocolatey 安裝 Guard。

1. 遵循本指南安裝 [Chocolatey](https://chocolatey.org/install)

1. 安裝 Chocolatey 後，請執行下列命令來安裝 Guard。

   ```
   choco install cloudformation-guard
   ```

   *結果*：您已成功安裝 Guard。

   1. （選用） 若要確認 Guard 的安裝，請執行下列命令。

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

     命令會傳回下列輸出：

     ```
     cfn-guard 3.1.2
     ```

# 安裝 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` – 規則集檔案的字串版本