

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

# 搭配 AWS CloudFormation Guard 規則使用輸入參數
<a name="using-input-parameters"></a>

AWS CloudFormation Guard 可讓您在驗證期間使用輸入參數進行動態資料查詢。當您需要參考規則中的外部資料時，此功能特別有用。不過，指定輸入參數索引鍵時，Guard 要求沒有衝突的路徑。

## 如何使用
<a name="how-to-use-input-parameters"></a>

1. 使用 `--input-parameters`或 `-i`旗標來指定包含輸入參數的檔案。您可以指定多個輸入參數檔案，並將合併以形成共同內容。輸入參數索引鍵不能有衝突的路徑。

1. 使用 `--data`或 `-d`旗標指定要驗證的實際範本檔案。

## 範例使用方式
<a name="input-parameters-example-usage"></a>

1. 建立輸入參數檔案 （例如 `network.yaml`)：

   ```
   NETWORK:
     allowed_security_groups: ["sg-282850", "sg-292040"]
     allowed_prefix_lists: ["pl-63a5400a", "pl-02cd2c6b"]
   ```

1. 在您的防護規則檔案中參考這些參數 （例如 `security_groups.guard`)：

   ```
   let groups = Resources.*[ Type == 'AWS::EC2::SecurityGroup' ]
   
   let permitted_sgs = NETWORK.allowed_security_groups
   let permitted_pls = NETWORK.allowed_prefix_lists
   rule check_permitted_security_groups_or_prefix_lists(groups) {
       %groups {
           this in %permitted_sgs or
           this in %permitted_pls
       }
   }
   
   rule CHECK_PERMITTED_GROUPS when %groups !empty {
       check_permitted_security_groups_or_prefix_lists(
          %groups.Properties.GroupName
       )
   }
   ```

1. 建立失敗的資料範本 （例如 `security_groups_fail.yaml`)：

   ```
   # ---
   # AWSTemplateFormatVersion: 2010-09-09
   # Description: CloudFormation - EC2 Security Group
   
   Resources:
     mySecurityGroup:
       Type: AWS::EC2::SecurityGroup
       Properties:
         GroupName: wrong
   ```

1. 執行驗證命令：

   ```
   cfn-guard validate -r security_groups.guard -i network.yaml -d security_groups_fail.yaml
   ```

   在此命令中：
   + `-r` 會指定規則檔案。
   + `-i` 指定輸入參數檔案。
   + `-d` 指定要驗證的資料檔案 （範本）。

## 多個輸入參數
<a name="multiple-input-parameters"></a>

您可以指定多個輸入參數檔案：

```
cfn-guard validate -r rules.guard -i params1.yaml -i params2.yaml -d template.yaml
```

使用 指定的所有檔案`-i`都會合併，以形成參數查詢的單一內容。