

這是新的 *CloudFormation 範本參考指南*。請更新您的書籤和連結。如需 CloudFormation 入門的說明，請參閱 [AWS CloudFormation 使用者指南](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)。

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

# 規則函數
<a name="intrinsic-function-reference-rules"></a>

規則函數是只在 CloudFormation 範本的 `Rules` 區段中運作的特殊函數。這些函數可協助您使用自訂邏輯來驗證參數值。所有驗證都會在 CloudFormation 建立或更新任何資源之前進行。

當標準參數限制不足時，規則很有用。例如，啟用 SSL 時，必須提供憑證和網域名稱。規則可以確保滿足這些相依性。

可以在規則的條件或宣告中使用內部函數，如 `Fn::Equals`、`Fn::Not`、`Fn::RefAll`。由條件屬性決定 CloudFormation 是否套用宣告。若條件計算結果為 `true`，CloudFormation 會評估宣告以驗證在建立或更新堆疊時，某個參數值是否有效。若參數值為無效，CloudFormation 便不會建立或更新堆疊。如果條件評估為 `false`，CloudFormation 便不會檢查參數值，而直接繼續運作堆疊。

如果您是第一次在範本中使用規則，建議您先檢閱**《AWS CloudFormation 使用者指南》中的 [CloudFormation 範本 Rules 語法](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/rules-section-structure.html)主題。

**Topics**
+ [`Fn::And`](#fn-and)
+ [`Fn::Contains`](#fn-contains)
+ [`Fn::EachMemberEquals`](#fn-eachmemberequals)
+ [`Fn::EachMemberIn`](#fn-eachmemberin)
+ [`Fn::Equals`](#fn-equals)
+ [`Fn::Not`](#fn-not)
+ [`Fn::Or`](#fn-or)
+ [`Fn::RefAll`](#fn-refall)
+ [`Fn::ValueOf`](#fn-valueof)
+ [`Fn::ValueOfAll`](#fn-valueofall)
+ [支援的函數](#supported-rule-functions)
+ [支援的屬性](#rules-parameter-attributes)

## `Fn::And`
<a name="fn-and"></a>

如果所有指定條件計算結果為 `true`，則傳回 `true`；如果其中任一條件計算結果為 `false`，則傳回 `false`。`Fn::And` 擔任 AND 運算子。可納入的條件數量下限為 2，上限為 10。

### 宣告
<a name="fn-and-declaration"></a>

```
"Fn::And" : [{condition}, {...}]
```

### Parameters
<a name="fn-and-parameters"></a>

*condition*  
規則指定的內部函數，計算結果為 `true` 或 `false`。

### 範例
<a name="fn-and-example"></a>

若參考的安全群組名稱等於 `true` 且 `sg-mysggroup` 參數的值為 `InstanceType` 或 `t3.large`，以下範例會計算結果為 `t3.small`：

```
"Fn::And": [
  {
    "Fn::Equals": [
      "sg-mysggroup",
      {"Ref": "ASecurityGroup"}
    ]
  },
  {
    "Fn::Contains": [
      [
        "t3.large",
        "t3.small"
      ],
      {"Ref": "InstanceType"}
    ]
  }
]
```

## `Fn::Contains`
<a name="fn-contains"></a>

若指定字串與字串清單其中至少一個值相符，則傳回 `true`。

### 宣告
<a name="fn-contains-declaration"></a>

```
"Fn::Contains" : [[list_of_strings], string]
```

### Parameters
<a name="fn-contains-parameters"></a>

*list\$1of\$1strings*  
字串清單，如 `"A", "B", "C"`。

*string*  
想與字串清單對照的字串，例如 `"A"`。

### 範例
<a name="fn-contains-example"></a>

若清單 (`true` 或 `InstanceType`) 含有 `t3.large` 參數值，以下函數會計算結果為 `t3.small`：

```
"Fn::Contains" : [
  ["t3.large", "t3.small"], {"Ref" : "InstanceType"}
]
```

## `Fn::EachMemberEquals`
<a name="fn-eachmemberequals"></a>

若指定字串符合某個清單內所有的值，傳回 `true`。

### 宣告
<a name="fn-eachmemberequals-declaration"></a>

```
"Fn::EachMemberEquals" : [[list_of_strings], string]
```

### Parameters
<a name="fn-eachmemberequals-parameters"></a>

*list\$1of\$1strings*  
字串清單，如 `"A", "B", "C"`。

*string*  
想與字串清單對照的字串，例如 `"A"`。

### 範例
<a name="fn-eachmemberequals-example"></a>

如果類型 `AWS::EC2::VPC::Id` 之所有參數適用的 `Department` 標籤具有 `IT` 值，則下列函數會傳回 `true`:

```
"Fn::EachMemberEquals" : [
  {"Fn::ValueOfAll" : ["AWS::EC2::VPC::Id", "Tags.Department"]}, "IT"
]
```

## `Fn::EachMemberIn`
<a name="fn-eachmemberin"></a>

若字串清單中的每個成員均符合第二份字串清單內的值少一個值，即傳回 `true`。

### 宣告
<a name="fn-eachmemberin-declaration"></a>

```
"Fn::EachMemberIn" : [[strings_to_check], [strings_to_match]]
```

### Parameters
<a name="fn-eachmemberin-parameters"></a>

*strings\$1to\$1check*  
字串清單，如 `"A", "B", "C"`。CloudFormation 會檢查 `strings_to_check` 參數內的每個成員是否存在於 `strings_to_match` 參數內。

*strings\$1to\$1match*  
字串清單，如 `"A", "B", "C"`。`strings_to_match` 參數的每個成員會與 `strings_to_check` 參數的成員進行對照。

### 範例
<a name="fn-eachmemberin-example"></a>

以下函數會檢查使用者是否指定了位在有效的 Virtual Private Cloud (VPC) 內的子網路。VPC 必須位在使用者使用堆疊的帳戶和區域內。該函數會套用至類型 `AWS::EC2::Subnet::Id` 的所有參數。

```
"Fn::EachMemberIn" : [ 
  {"Fn::ValueOfAll" : ["AWS::EC2::Subnet::Id", "VpcId"]}, {"Fn::RefAll" : "AWS::EC2::VPC::Id"}
]
```

## `Fn::Equals`
<a name="fn-equals"></a>

比較兩個值，判斷是否相同。若兩值相同，即傳回 `true`，反之則傳回 `false`。

### 宣告
<a name="fn-equals-declaration"></a>

```
"Fn::Equals" : ["value_1", "value_2"]
```

### Parameters
<a name="fn-equals-parameters"></a>

*`value`*  
要與另一個值比較的任何類型的值。

### 範例
<a name="fn-equals-example"></a>

若 `true` 參數的值等同 `EnvironmentType`，下列範例會計算結果為 `prod`：

```
"Fn::Equals" : [{"Ref" : "EnvironmentType"}, "prod"]
```

## `Fn::Not`
<a name="fn-not"></a>

若某個條件計算結果為 `false`，則傳回 `true`，若某個條件計算結果為 `true`，則傳回 `false`。`Fn::Not` 擔任 NOT 運算子。

### 宣告
<a name="fn-not-declaration"></a>

```
"Fn::Not" : [{condition}]
```

### Parameters
<a name="fn-not-parameters"></a>

*`condition`*  
規則指定的內部函數，計算結果為 `true` 或 `false`。

### 範例
<a name="fn-not-example"></a>

若 `EnvironmentType` 參數的值不同於 `prod`，下列範例會計算結果為 `true`：

```
"Fn::Not" : [{"Fn::Equals" : [{"Ref" : "EnvironmentType"}, "prod"]}]
```

## `Fn::Or`
<a name="fn-or"></a>

如果任一指定條件評估為 `true`，則傳回 `true`；如果所有的條件評估為 `false`，則傳回 `false`。`Fn::Or` 擔任 OR 運算子。可納入的條件數量下限為 2，上限為 10。

### 宣告
<a name="fn-or-declaration"></a>

```
"Fn::Or" : [{condition}, {...}]
```

### Parameters
<a name="fn-or-parameters"></a>

*`condition`*  
規則指定的內部函數，計算結果為 `true` 或 `false`。

### 範例
<a name="fn-or-example"></a>

若參考的安全群組名稱等於 `true` 或 `sg-mysggroup` 參數的值為 `InstanceType` 或 `t3.large`，以下範例會計算結果為 `t3.small`：

```
"Fn::Or" : [
  {"Fn::Equals" : ["sg-mysggroup", {"Ref" : "ASecurityGroup"}]},
  {"Fn::Contains" : [["t3.large", "t3.small"], {"Ref" : "InstanceType"}]}
]
```

## `Fn::RefAll`
<a name="fn-refall"></a>

傳回指定參數類型的所有值。

### 宣告
<a name="fn-refall-declaration"></a>

```
"Fn::RefAll" : "parameter_type"
```

### Parameters
<a name="fn-refall-parameters"></a>

*parameter\$1type*  
 AWS特定參數類型，例如 `AWS::EC2::SecurityGroup::Id`或 `AWS::EC2::VPC::Id`。如需詳細資訊，請參閱*AWS CloudFormation 《 使用者指南*》中的[支援 AWS的特定參數類型](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-supplied-parameter-types.html#aws-specific-parameter-types-supported)。

### 範例
<a name="fn-refall-example"></a>

下列函數會傳回建立或更新堆疊之 區域和 AWS 帳戶 的所有 VPC IDs 清單：

```
"Fn::RefAll" : "AWS::EC2::VPC::Id"
```

## `Fn::ValueOf`
<a name="fn-valueof"></a>

傳回某個屬性值，或指定參數和屬性值的清單。

### 宣告
<a name="fn-valueof-declaration"></a>

```
"Fn::ValueOf" : [ "parameter_logical_id", "attribute" ]
```

### Parameters
<a name="fn-valueof-parameters"></a>

*屬性*  
想要從中擷取某個值的屬性名稱。如需屬性的詳細資訊，請參閱 [支援的屬性](#rules-parameter-attributes)。

*parameter\$1logical\$1id*  
想要從中擷取屬性值的參數名稱。該參數並需於範本的 `Parameters` 區段內宣告。

### 範例
<a name="fn-valueof-examples"></a>

以下範例會傳回由 `Department` 參數指定之 VPC 的 `ElbVpc` 標籤的值：

```
"Fn::ValueOf" : ["ElbVpc", "Tags.Department"]
```

若給某個參數指定了多個值，Fn::ValueOf function 可傳回清單。例如可指定多個子網路，並取得可用區域清單，其中每個成員均是特定子網路的可用區域：

```
"Fn::ValueOf" : ["ListOfElbSubnets", "AvailabilityZone"]
```

## `Fn::ValueOfAll`
<a name="fn-valueofall"></a>

傳回指定參數類型和屬性所有屬性值的清單。

### 宣告
<a name="fn-valueofall-declaration"></a>

```
"Fn::ValueOfAll" : ["parameter_type", "attribute"]
```

### Parameters
<a name="fn-valueofall-parameters"></a>

*屬性*  
想要從中擷取某個值的屬性名稱。如需屬性的詳細資訊，請參閱 [支援的屬性](#rules-parameter-attributes)。

*parameter\$1type*  
 AWS特定參數類型，例如 `AWS::EC2::SecurityGroup::Id`或 `AWS::EC2::VPC::Id`。如需詳細資訊，請參閱*AWS CloudFormation 《 使用者指南*》中的[支援 AWS的特定參數類型](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-supplied-parameter-types.html#aws-specific-parameter-types-supported)。

### 範例
<a name="fn-valueofall-example"></a>

在下列範例中，`Fn::ValueOfAll` 函數會傳回一份值的清單，其中每個成員均是含有 `Department` 標籤之 VPC 該標籤的值。

```
"Fn::ValueOfAll" : ["AWS::EC2::VPC::Id", "Tags.Department"]
```

## 支援的函數
<a name="supported-rule-functions"></a>

無法在 `Fn::ValueOf` 和 `Fn::ValueOfAll` 函數內使用另一個函數。不過可以在所有其他規則特定的內建函式使用下列函數：
+ `Ref`
+ 其他規則特定的內部函數

## 支援的屬性
<a name="rules-parameter-attributes"></a>

以下清單所述的是可為特定資源和參數類型擷取的屬性值：

`AWS::EC2::VPC::Id` 參數類型或 VPC ID。  
+ DefaultNetworkAcl
+ DefaultSecurityGroup
+ 標籤。*tag\$1key*

`AWS::EC2::Subnet::Id` 參數類型或子網路 ID。  
+ AvailabilityZone
+ 標籤。*tag\$1key*
+ VpcId

`AWS::EC2::SecurityGroup::Id` 參數類型或安全群組 ID。  
+ 標籤。*tag\$1key*