條件函數 - AWS CloudFormation

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

條件函數

您可以使用內部函數 (例如 Fn::IfFn::EqualsFn::Not) 按條件建立堆疊資源。這些條件的計算是根據您在建立或更新堆疊時,宣告的輸入參數。定義所有條件之後,即可將這些條件與範本之資源和輸出區段中的資源或資源屬性建立關聯。

所有條件都是在範本的條件區段中定義,除了 Fn::If 條件以外。您可以在範本中資源區段和輸出區段中的中繼資料屬性、更新政策屬性和屬性值中使用 Fn::If 條件。

當您想要重複使用範本以在不同的內容中建立資源時 (例如測試環境與生產環境),可以使用條件。在範本中,您可以新增 EnvironmentType 輸入參數,此參數接受 prodtest 作為輸入。對於生產環境,您可以包含具有特定功能的 Amazon EC2 執行個體;但是,對於測試環境,您希望使用較少的功能來節省成本。使用條件,即可針對每種環境類型定義要建立哪些資源和其設定方式。

如需條件區段的詳細資訊,請參閱 Conditions

注意

您只能從範本的參數和映射區段中參考其他條件和值。例如,您可以參考輸入參數中的值,但您不能在條件中參考資源的邏輯 ID。

為條件建立關聯

若要按條件建立資源、資源屬性或輸出,您必須將其與條件建立關聯。將條件的Condition:索引鍵和邏輯 ID 新增為關聯條件的屬性,如下列程式碼片段所示。 AWS CloudFormation 只有當CreateProdResources條件評估為 true 時,才會建立NewVolume資源。

JSON

"NewVolume" : { "Type" : "AWS::EC2::Volume", "Condition" : "CreateProdResources", "Properties" : { "Size" : "100", "AvailabilityZone" : { "Fn::GetAtt" : [ "EC2Instance", "AvailabilityZone" ]} }

YAML

NewVolume: Type: "AWS::EC2::Volume" Condition: CreateProdResources Properties: Size: 100 AvailabilityZone: !GetAtt EC2Instance.AvailabilityZone

Fn::If

針對 Fn::If 函數,您只需要指定條件名稱。下列程式碼片段說明如何使用 Fn::If 按條件指定資源屬性。如果CreateLargeSize條件為 true,請將磁碟區大小 CloudFormation 設定為100。如果條件為 false,請將磁碟區大小 CloudFormation 設定為10

JSON

{ "NewVolume": { "Type": "AWS::EC2::Volume", "Properties": { "Size": { "Fn::If": [ "CreateLargeSize", "100", "10" ] }, "AvailabilityZone": { "Fn::GetAtt": [ "Ec2Instance", "AvailabilityZone" ] } }, "DeletionPolicy": "Snapshot" } }

YAML

NewVolume: Type: 'AWS::EC2::Volume' Properties: Size: 'Fn::If': - CreateLargeSize - '100' - '10' AvailabilityZone: 'Fn::GetAtt': - Ec2Instance - AvailabilityZone DeletionPolicy: Snapshot

巢狀條件

您也可以在其他條件中使用條件。下列程式碼片段來自範本的 Conditions 區段。MyAndCondition 條件包括 SomeOtherCondition 條件:

JSON
"MyAndCondition": { "Fn::And": [ {"Fn::Equals": ["sg-mysggroup", {"Ref": "ASecurityGroup"}]}, {"Condition": "SomeOtherCondition"} ] }
YAML
MyAndCondition: !And - !Equals ["sg-mysggroup", !Ref "ASecurityGroup"] - !Condition SomeOtherCondition

Fn::And

true如果所有指定的條件都評估為 true,則返回,或者false如果任何一個條件評估為 false,則返回。 Fn::And充當一個AND運營商。可納入的條件數目下限為 2,上限為 10。

宣告

JSON

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

YAML

完整函式名稱的語法:

Fn::And: [condition]

短格式的語法:

!And [condition]

參數

condition

計算結果為 truefalse 的條件。

範例

如果參考安全群組名稱等於 MyAndCondition,並且 sg-mysggroup 計算為 true 時,下列的 SomeOtherCondition 會計算為 true:

JSON

"MyAndCondition": { "Fn::And": [ {"Fn::Equals": ["sg-mysggroup", {"Ref": "ASecurityGroup"}]}, {"Condition": "SomeOtherCondition"} ] }

YAML

MyAndCondition: !And - !Equals ["sg-mysggroup", !Ref ASecurityGroup] - !Condition SomeOtherCondition

Fn::Equals

比較兩個值是否相等。若兩值相同,即傳回 true,反之則傳回 false

宣告

JSON

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

YAML

完整函式名稱的語法:

Fn::Equals: [value_1, value_2]

短格式的語法:

!Equals [value_1, value_2]

參數

value

您希望比較之任何類型的值。

範例

UseProdCondition 參數的值等於 EnvironmentType,下列 prod 條件會評估為 true:

JSON

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

YAML

UseProdCondition: !Equals [!Ref EnvironmentType, prod]

Fn::If

如果指定的條件計算為 true 則傳回一個值;如果指定的條件計算為 false 則傳回另一個值。目前, CloudFormation支援中繼資料屬性、更新原則屬性,以及範Fn::If本「資源」區段和「輸出」區段中的內建函數。您可以使用 AWS::NoValue 虛擬參數作為傳回值來移除對應的屬性。

宣告

YAML

完整函式名稱的語法:

Fn::If: [condition_name, value_if_true, value_if_false]

短格式的語法:

!If [condition_name, value_if_true, value_if_false]

參數

condition_name

條件區段中對條件的參考。使用條件的名稱來參考。

value_if_true

當指定的條件計算為 true 傳回的值。

value_if_false

當指定的條件計算為 false 傳回的值。

範例

若要檢視其他範例,請參閱 範例範本

範例 1

下列程式碼片段在SecurityGroups屬性中使用 Amazon EC2 資源的Fn::If函數。如果CreateNewSecurityGroup條件評估為 true,會 CloudFormation 使用的參考值NewSecurityGroup來指定SecurityGroups屬性;否則,會 CloudFormation 使用的參考值ExistingSecurityGroup

JSON
"SecurityGroups" : [{ "Fn::If" : [ "CreateNewSecurityGroup", {"Ref" : "NewSecurityGroup"}, {"Ref" : "ExistingSecurityGroup"} ] }]
YAML
SecurityGroups: - !If [CreateNewSecurityGroup, !Ref NewSecurityGroup, !Ref ExistingSecurityGroup]

範例 2

在範本的輸出區段中,您可以使用 Fn::If 函數來按條件輸出資訊。在下面的代碼片段中,如果CreateNewSecurityGroup條件評估為 true,則 CloudFormation 輸出NewSecurityGroup資源的安全組 ID。如果條件為 false,則 CloudFormation 輸出ExistingSecurityGroup資源的安全性群組識別碼。

JSON
"Outputs" : { "SecurityGroupId" : { "Description" : "Group ID of the security group used.", "Value" : { "Fn::If" : [ "CreateNewSecurityGroup", {"Ref" : "NewSecurityGroup"}, {"Ref" : "ExistingSecurityGroup"} ] } } }
YAML
Outputs: SecurityGroupId: Description: Group ID of the security group used. Value: !If [CreateNewSecurityGroup, !Ref NewSecurityGroup, !Ref ExistingSecurityGroup]

範例 3

下列程式碼片段在 AWS::NoValue 函數中使用 Fn::If 虛擬參數。只有在提供快照 ID 時,此條件才會為 Amazon RDS 資料庫執行個體使用快照。如果UseDBSnapshot條件評估為 true,則會 CloudFormation 使用DBSnapshotIdentifier屬性的DBSnapshotName參數值。如果條件評估為 false,則會 CloudFormation 移除該DBSnapshotIdentifier屬性。

JSON
"MyDB" : { "Type" : "AWS::RDS::DBInstance", "Properties" : { "AllocatedStorage" : "5", "DBInstanceClass" : "db.t2.small", "Engine" : "MySQL", "EngineVersion" : "5.5", "MasterUsername" : { "Ref" : "DBUser" }, "MasterUserPassword" : { "Ref" : "DBPassword" }, "DBParameterGroupName" : { "Ref" : "MyRDSParamGroup" }, "DBSnapshotIdentifier" : { "Fn::If" : [ "UseDBSnapshot", {"Ref" : "DBSnapshotName"}, {"Ref" : "AWS::NoValue"} ] } } }
YAML
MyDB: Type: "AWS::RDS::DBInstance" Properties: AllocatedStorage: 5 DBInstanceClass: db.t2.small Engine: MySQL EngineVersion: 5.5 MasterUsername: !Ref DBUser MasterUserPassword: !Ref DBPassword DBParameterGroupName: !Ref MyRDSParamGroup DBSnapshotIdentifier: !If [UseDBSnapshot, !Ref DBSnapshotName, !Ref "AWS::NoValue"]

範例 4

下列程式碼片段只有在 RollingUpdates 條件評估為 true 時,才會提供 Auto Scaling 更新政策。如果條件評估為 false,則會 CloudFormation 移除AutoScalingRollingUpdate更新原則。

JSON
"UpdatePolicy": { "AutoScalingRollingUpdate": { "Fn::If": [ "RollingUpdates", { "MaxBatchSize": "2", "MinInstancesInService": "2", "PauseTime": "PT0M30S" }, { "Ref" : "AWS::NoValue" } ] } }
YAML
UpdatePolicy: AutoScalingRollingUpdate: !If - RollingUpdates - MaxBatchSize: 2 MinInstancesInService: 2 PauseTime: PT0M30S - !Ref "AWS::NoValue"

Fn::Not

true對評估為的條件,傳回評估false為的條件false或傳回的條件trueFn::Not充當NOT運營商。

宣告

JSON

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

YAML

完整函式名稱的語法:

Fn::Not: [condition]

短格式的語法:

!Not [condition]

參數

condition

計算為 Fn::Equalstrue 的條件 (例如 false)。

範例

EnvCondition 參數的值等於 EnvironmentType,下列 prod 條件會評估為 true:

JSON

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

YAML

MyNotCondition: !Not [!Equals [!Ref EnvironmentType, prod]]

Fn::Or

如果任一指定條件評估為 true,則傳回 true;如果所有條件評估為 false,則傳回 falseFn::Or 作為 OR 運算子使用。可納入的條件數目下限為 2,上限為 10。

宣告

JSON

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

YAML

完整函式名稱的語法:

Fn::Or: [condition, ...]

短格式的語法:

!Or [condition, ...]

參數

condition

計算結果為 truefalse 的條件。

範例

如果參考安全群組名稱等於 MyOrCondition,或 sg-mysggroup 計算為 true 時,下列的 SomeOtherCondition 會計算為 true:

JSON

"MyOrCondition" : { "Fn::Or" : [ {"Fn::Equals" : ["sg-mysggroup", {"Ref" : "ASecurityGroup"}]}, {"Condition" : "SomeOtherCondition"} ] }

YAML

MyOrCondition: !Or [!Equals [sg-mysggroup, !Ref ASecurityGroup], Condition: SomeOtherCondition]

支援的函數

您可以在 Fn::If 條件中使用下列函數:

  • Fn::Base64

  • Fn::FindInMap

  • Fn::GetAtt

  • Fn::GetAZs

  • Fn::If

  • Fn::Join

  • Fn::Select

  • Fn::Sub

  • Ref

您可以在所有其他條件函數中使用下列函數,例如 Fn::EqualsFn::Or

  • Fn::FindInMap

  • Ref

  • 其他條件函數