這是新的 AWS CloudFormation 範本參考指南。請更新您的書籤和連結。如需開始使用 CloudFormation 的說明,請參閱 AWS CloudFormation 使用者指南。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
條件函數
您可以使用內部函數,例如 Fn::If
或 Fn::Equals
,根據條件式邏輯建立和設定堆疊資源。這些條件會在堆疊建立或更新期間評估。定義所有條件之後,您可以將它們與範本的 Resources
和 Outputs
區段中的資源或資源屬性建立關聯。
對於進階案例,您可以使用 Fn::And
或 Fn::Or
函數結合條件,或使用 Fn::Not
來否定條件的值。您也可以巢狀條件來建立更複雜的條件式邏輯。
如果您是第一次在 範本中使用條件,建議您先檢閱AWS CloudFormation 《 使用者指南》中的 CloudFormation 範本條件語法主題。
注意
您必須定義範本 Conditions
區段中的所有條件,條件除外Fn::If
。您可以在 Metadata
和 Resources
Outputs
區段的 屬性、 UpdatePolicy
屬性和 屬性值中使用 Fn::If
條件。
Fn::And
如果所有指定條件評估為 true,則傳回 true
;如果其中任一條件評估為 false,則傳回 false
。Fn::And
作為 AND 運算子使用。可納入的條件數目下限為 2,上限為 10。
宣告
JSON
"Fn::And": [{
condition
}, {...
}]
參數
- condition
-
計算結果為
true
或false
的條件。
Fn::And
使用範例
如果參考安全群組名稱等於 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
-
您要比較的字串值。
Fn::Equals
使用範例
若 IsProduction
參數的值等於 EnvironmentType
,下列 prod
條件會評估為 true:
JSON
"IsProduction" : { "Fn::Equals": [ {"Ref": "EnvironmentType"}, "prod" ] }
YAML
IsProduction: !Equals [!Ref EnvironmentType, prod]
Fn::If
如果指定的條件計算為 true
則傳回一個值;如果指定的條件計算為 false
則傳回另一個值。目前,CloudFormation 在範本的 Resources
區段和 Outputs
區段的 Metadata
屬性、 UpdatePolicy
屬性和 屬性值中支援Fn::If
內部 函數。您可以使用 AWS::NoValue
虛擬參數作為傳回值來移除對應的屬性。
宣告
JSON
"Fn::If": [
condition_name
,value_if_true
,value_if_false
]
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
傳回的值。
Fn::If
使用範例
有條件地選擇資源
下列範例在 Amazon EC2 資源定義中使用 Fn::If
函數,以決定要與執行個體建立關聯的安全群組資源。如果CreateNewSecurityGroup
條件評估為 true,CloudFormation 會使用 的參考值 NewSecurityGroup
(範本中其他位置建立的安全群組) 來指定 SecurityGroupIds
屬性。如果 CreateNewSecurityGroup
為 false,CloudFormation 會使用 的參考值 ExistingSecurityGroupId
(參考現有安全群組的 參數)。
JSON
"Resources": { "EC2Instance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": "ami-0abcdef1234567890", "InstanceType": "t3.micro", "SecurityGroupIds": { "Fn::If": [ "CreateNewSecurityGroup", [{"Ref": "NewSecurityGroup"}], [{"Ref": "ExistingSecurityGroupId"}] ] }] } } }
YAML
Resources: EC2Instance: Type: AWS::EC2::Instance Properties: ImageId: ami-0abcdef1234567890 InstanceType: t3.micro SecurityGroupIds: !If - CreateNewSecurityGroup - [!Ref NewSecurityGroup] - [!Ref ExistingSecurityGroupId]
條件式輸出
在範本的 Output
區段中,您可以使用 Fn::If
函數條件式輸出資訊。在下列程式碼片段中,如果 CreateNewSecurityGroup
條件評估為 true,則 CloudFormation 會輸出 NewSecurityGroup
資源的安全群組 ID。如果條件為 false,則 ExistingSecurityGroup
會輸出 CloudFormation 資源的安全群組 ID。
JSON
"Outputs" : { "SecurityGroupId" : { "Description" : "Group ID of the security group used.", "Value" : { "Fn::If" : [ "CreateNewSecurityGroup", {"Ref" : "NewSecurityGroup"}, {"Ref" : "ExistingSecurityGroupId"} ] } } }
YAML
Outputs: SecurityGroupId: Description: Group ID of the security group used. Value: !If [CreateNewSecurityGroup, !Ref NewSecurityGroup, !Ref ExistingSecurityGroupId]
條件式陣列值
下列範例使用 Fn::If
,根據條件有條件地提供不同的陣列值。如果MoreThan2AZs
條件評估為 true,則會使用三個公有子網路。否則,它只會使用兩個公有子網路。
JSON
"Subnets": { "Fn::If": [ "MoreThan2AZs", [ {"Fn::ImportValue": "PublicSubnet01"}, {"Fn::ImportValue": "PublicSubnet02"}, {"Fn::ImportValue": "PublicSubnet03"} ], [ {"Fn::ImportValue": "PublicSubnet01"}, {"Fn::ImportValue": "PublicSubnet02"} ] ] }
YAML
Subnets: Fn::If: - MoreThan2AZs - - Fn::ImportValue: PublicSubnet01 - Fn::ImportValue: PublicSubnet02 - Fn::ImportValue: PublicSubnet03 - - Fn::ImportValue: PublicSubnet01 - Fn::ImportValue: PublicSubnet02
條件式屬性和屬性值
下列範例在 AWS::NoValue
Fn::If
函數中使用虛擬參數。只有在提供快照 ID 時,該條件才會為 Amazon RDS 資料庫執行個體使用快照。若 UseDBSnapshot
條件評估為 true,CloudFormation 會對 DBSnapshotIdentifier
屬性使用 DBSnapshotName
參數值。若條件評估為 false,CloudFormation 即會移除 DBSnapshotIdentifier
屬性。
它也會在 Amazon RDS 資料庫執行個體的 AllocatedStorage
屬性中使用 Fn::If
函數。如果IsProduction
條件評估為 true,儲存大小會設為 100
。否則,它會設定為 20
。
JSON
"MyDatabase" : { "Type" : "AWS::RDS::DBInstance", "Properties": { "DBInstanceClass": "db.t3.micro", "AllocatedStorage": { "Fn::If": [ "IsProduction", 100, 20 ] }, "Engine" : "MySQL", "EngineVersion" : "5.5", "MasterUsername" : { "Ref" : "DBUser" }, "MasterUserPassword" : { "Ref" : "DBPassword" }, "DBParameterGroupName" : { "Ref" : "MyRDSParamGroup" }, "DBSnapshotIdentifier" : { "Fn::If" : [ "UseDBSnapshot", {"Ref" : "DBSnapshotName"}, {"Ref" : "AWS::NoValue"} ] } } }
YAML
MyDatabase: Type: AWS::RDS::DBInstance Properties: DBInstanceClass: db.t3.micro AllocatedStorage: !If [IsProduction, 100, 20] Engine: MySQL EngineVersion: 5.5 MasterUsername: !Ref DBUser MasterUserPassword: !Ref DBPassword DBParameterGroupName: !Ref MyRDSParamGroup DBSnapshotIdentifier: !If [UseDBSnapshot, !Ref DBSnapshotName, !Ref "AWS::NoValue"]
條件式更新政策
下列程式碼片段只有在 RollingUpdates
條件評估為 true 時,才會提供 Auto Scaling 更新政策。如果條件評估為 false,則 CloudFormation 會移除 AutoScalingRollingUpdate
更新政策。
JSON
"UpdatePolicy": { "Fn::If": [ "RollingUpdates", { "AutoScalingRollingUpdate": { "MaxBatchSize": 2, "MinInstancesInService": 2, "PauseTime": "PT0M30S" } }, { "Ref": "AWS::NoValue" } ] }
YAML
UpdatePolicy: !If - RollingUpdates - AutoScalingRollingUpdate: MaxBatchSize: 2 MinInstancesInService: 2 PauseTime: PT0M30S - !Ref "AWS::NoValue"
Fn::Not
若條件評估為 false
,則傳回 true
,若條件評估為 true
,則傳回 false
。Fn::Not
作為 NOT 運算子使用。
宣告
JSON
"Fn::Not": [{
condition
}]
參數
- condition
-
計算為
Fn::Equals
或true
的條件 (例如false
)。
Fn::Not
使用範例
若 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,則傳回 false
。Fn::Or
作為 OR 運算子使用。可納入的條件數目下限為 2,上限為 10。
宣告
JSON
"Fn::Or": [{
condition
}, {...
}]
YAML
完整函式名稱的語法:
Fn::Or: [
condition, ...
]
短格式的語法:
!Or [
condition, ...
]
參數
- condition
-
計算結果為
true
或false
的條件。
Fn::Or
使用範例
如果參考安全群組名稱等於 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::Equals
和 Fn::Or
:
-
Fn::FindInMap
-
Ref
-
其他條件函數
範例範本
按條件為生產、開發或測試堆疊來建立資源
在某些情況下,建議您建立類似但略有不同的堆疊。例如,您可能有一個讓您用於生產應用程式的範本。您想要建立相同的生產堆疊,所以您可以使用它來進行開發或測試。不過,針對開發和測試,您不一定需要包含在生產層級堆疊中的所有額外容量。反之,您可以使用環境類型輸入參數,按條件建立特定於資源生產、開發或測試的堆疊資源,如下例所示:
您可以為 prod
參數指定 dev
、test
或 EnvType
。針對每種環境類型,範本都會指定不同的執行個體類型。執行個體類型可以從大型、運算優化執行個體,到小型一般用途執行個體類型。為了有條件地指定執行個體類型,範本會在範本的 Conditions
區段中定義兩個條件:CreateProdResources
,如果EnvType
參數值等於 ,則評估為 trueprod
CreateDevResources
;如果參數值等於 ,則評估為 truedev
。
在 InstanceType
屬性中,範本會巢狀化兩個 Fn::If
內部函數來判斷要使用哪個執行個體類型。如果 CreateProdResources
條件為 true,則執行個體類型為 c5.xlarge
。如果條件為 false,則計算 CreateDevResources
條件。如果 CreateDevResources
條件為 true,則執行個體類型為 t3.medium
;否則執行個體類型為 t3.small
。
除了執行個體類型之外,生產環境也會為執行個體建立並連接一個 Amazon EC2 磁碟區。MountPoint
和 NewVolume
資源與 CreateProdResources
條件相關聯,因此僅在條件計算為 true 時才會建立資源。
範例 JSON
{ "AWSTemplateFormatVersion" : "2010-09-09", "Parameters" : { "EnvType" : { "Description" : "Environment type.", "Default" : "test", "Type" : "String", "AllowedValues" : ["prod", "dev", "test"], "ConstraintDescription" : "must specify prod, dev, or test." } }, "Conditions" : { "CreateProdResources" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "prod"]}, "CreateDevResources" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "dev"]} }, "Resources" : { "EC2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "ImageId" : "ami-1234567890abcdef0", "InstanceType" : { "Fn::If" : [ "CreateProdResources", "c5.xlarge", {"Fn::If" : [ "CreateDevResources", "t3.medium", "t3.small" ]} ]} } }, "MountPoint" : { "Type" : "AWS::EC2::VolumeAttachment", "Condition" : "CreateProdResources", "Properties" : { "InstanceId" : { "Ref" : "EC2Instance" }, "VolumeId" : { "Ref" : "NewVolume" }, "Device" : "/dev/sdh" } }, "NewVolume" : { "Type" : "AWS::EC2::Volume", "Condition" : "CreateProdResources", "Properties" : { "Size" : "100", "AvailabilityZone" : { "Fn::GetAtt" : [ "EC2Instance", "AvailabilityZone" ]} } } } }
範例 YAML
AWSTemplateFormatVersion: "2010-09-09" Parameters: EnvType: Description: Environment type. Default: test Type: String AllowedValues: [prod, dev, test] ConstraintDescription: must specify prod, dev, or test. Conditions: CreateProdResources: !Equals [!Ref EnvType, prod] CreateDevResources: !Equals [!Ref EnvType, "dev"] Resources: EC2Instance: Type: AWS::EC2::Instance Properties: ImageId: ami-1234567890abcdef0 InstanceType: !If [CreateProdResources, c5.xlarge, !If [CreateDevResources, t3.medium, t3.small]] MountPoint: Type: AWS::EC2::VolumeAttachment Condition: CreateProdResources Properties: InstanceId: !Ref EC2Instance VolumeId: !Ref NewVolume Device: /dev/sdh NewVolume: Type: AWS::EC2::Volume Condition: CreateProdResources Properties: Size: 100 AvailabilityZone: !GetAtt EC2Instance.AvailabilityZone
注意
如需使用條件來建立資源的更複雜範例,請參閱 Condition 屬性主題。