本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
您可以使用內部函數 (例如 Fn::If
、Fn::Equals
和 Fn::Not
) 按條件建立堆疊資源。這些條件的計算是根據您在建立或更新堆疊時,宣告的輸入參數。定義所有條件之後,即可將這些條件與範本之資源和輸出區段中的資源或資源屬性建立關聯。
所有條件都是在範本的條件區段中定義,除了 Fn::If
條件以外。您可以在範本中資源區段和輸出區段中的中繼資料屬性、更新政策屬性和屬性值中使用 Fn::If
條件。
當您想要重複使用範本以在不同的內容中建立資源時 (例如測試環境與生產環境),可以使用條件。在範本中,您可以新增 EnvironmentType
輸入參數,此參數接受 prod
或 test
作為輸入。針對生產環境,您可以讓 Amazon EC2 執行個體包含特定功能;不過,在測試環境中,就要使用較少的功能來節省成本。使用條件,即可針對每種環境類型定義要建立哪些資源和其設定方式。
如需條件區段的詳細資訊,請參閱 CloudFormation 範本Conditions語法。
注意
您只能從範本的參數和映射區段中參考其他條件和值。例如,您可以參考輸入參數中的值,但您不能在條件中參考資源的邏輯 ID。
為條件建立關聯
若要按條件建立資源、資源屬性或輸出,您必須將其與條件建立關聯。新增 Condition:
金鑰和條件的邏輯 ID 做為屬性來關聯條件,如下列程式碼片段所示。 僅在CreateProdResources
條件評估為 true 時 AWS CloudFormation 建立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
}, {...
}]
參數
condition
-
計算結果為
true
或false
的條件。
範例
如果參考安全群組名稱等於 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
虛擬參數作為傳回值來移除對應的屬性。
宣告
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
傳回的值。
範例
若要檢視其他範例,請參閱 範例範本。
範例 1
下列程式碼片段在 Amazon EC2 資源的 SecurityGroups
屬性中使用 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,則 ExistingSecurityGroup
會輸出 CloudFormation 資源的安全群組 ID。
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
若條件評估為 false
,則傳回 true
,若條件評估為 true
,則傳回 false
。Fn::Not
作為 NOT 運算子使用。
宣告
JSON
"Fn::Not": [{condition
}]
參數
condition
-
計算為
Fn::Equals
或true
的條件 (例如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,則傳回 false
。Fn::Or
作為 OR 運算子使用。可納入的條件數目下限為 2,上限為 10。
宣告
JSON
"Fn::Or": [{condition
}, {...
}]
YAML
完整函式名稱的語法:
Fn::Or: [condition, ...
]
短格式的語法:
!Or [condition, ...
]
參數
condition
-
計算結果為
true
或false
的條件。
範例
如果參考安全群組名稱等於 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
-
其他條件函數